2

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"
    },
];

3 Answers 3

1
  • 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);

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.