Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
How to filter an array data based on another array value?

I have array data like these.

const dataSample = [
    {
      "staff": 1000,
      "name": "Apple",
      "logo_url": "biten apple",
      "down":"yes"
    },
    {
      "staff": 500,
      "name": "Microsoft",
      "logo": "squares",
      "down":"no"
    },
    {
      "staff": 4000,
      "name": "Google",
      "logo_url": "letter",
      "down":"no"
    },
    {
      "staff": 2000,
      "name": "Coca Cola",
      "logo": "letters",
      "down":"yes"
    },
  ];

I have a another array

const filterArr = [“name”, “down”];

I want a output like these?

[
    {
      "name": "Apple",
      "down": "yes"
    },
    {
      "name": "Microsoft",
      "down": "no"
    },
    {
      "name": "Google",
      "down": "no"
    },
    {
      "name": "Coca Cola",
      "down": "yes"
    },
];
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

  • Map over the original array, dataSample in your case.
  • Convert every object into an array using Object.entries and filter out the items where the key is present in the lookup array (filterArr).
  • Convert the array back to an object using Object.fromEntries

const dataSample = [
    { staff: 1000, name: "Apple", logo_url: "biten apple", down: "yes" },
    { staff: 500, name: "Microsoft", logo: "squares", down: "no" },
    { staff: 4000, name: "Google", logo_url: "letter", down: "no" },
    { staff: 2000, name: "Coca Cola", logo: "letters", down: "yes" },
  ],
  filterArr = ["name", "down"],
  output = dataSample.map((o) =>
    Object.fromEntries(
      Object.entries(o).filter(([k, v]) => filterArr.includes(k))
    )
  );

console.log(output);

If you're dealing with large arrays, then you should consider converting the lookup array (filterArr) into a Set.

const dataSample = [
    { staff: 1000, name: "Apple", logo_url: "biten apple", down: "yes" },
    { staff: 500, name: "Microsoft", logo: "squares", down: "no" },
    { staff: 4000, name: "Google", logo_url: "letter", down: "no" },
    { staff: 2000, name: "Coca Cola", logo: "letters", down: "yes" },
  ],
  filterArr = ["name", "down"],
  filterSet = new Set(filterArr),
  output = dataSample.map((o) =>
    Object.fromEntries(Object.entries(o).filter(([k, v]) => filterSet.has(k)))
  );

console.log(output);

about 4 years ago · Juan Pablo Isaza Report

0

This can be done using Array.map() combined with Array.reduce() as follows:

const result = dataSample.map(o => filterArr.reduce((acc, curr) => {
  acc[curr] = o[curr];
  return acc;
}, {}));

Please take a look at the runnable code below and see how it works.

const dataSample = [{
    "staff": 1000,
    "name": "Apple",
    "logo_url": "biten apple",
    "down": "yes"
  },
  {
    "staff": 500,
    "name": "Microsoft",
    "logo": "squares",
    "down": "no"
  },
  {
    "staff": 4000,
    "name": "Google",
    "logo_url": "letter",
    "down": "no"
  },
  {
    "staff": 2000,
    "name": "Coca Cola",
    "logo": "letters",
    "down": "yes"
  },
];
const filterArr = ["name", "down"];

const result = dataSample.map(o => filterArr.reduce((acc, curr) => {
  acc[curr] = o[curr];
  return acc;
}, {}));

console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

try this:

const dataSample = [
    { staff: 1000, name: "Apple", logo_url: "biten apple", down: "yes" },
    { staff: 500, name: "Microsoft", logo: "squares", down: "no" },
    { staff: 4000, name: "Google", logo_url: "letter", down: "no" },
    { staff: 2000, name: "Coca Cola", logo: "letters", down: "yes" },
];
const filterArr = ["name", "down"];
let output = dataSample.map((cv) => Object.assign( ...filterArr.map(fa => ({[fa]: cv[fa]}))));
console.log(output);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!