-1

I created an array like below:

arrayObject = [{one: "1", two: "2", three: "3", four: "4", five: "5"}, 
               {one: "1", two: "2", three: "3", four: "4", five: "5"}];

How can I get/filter that array to had arrayObject only with "one", "two" and "five" element like below:

arrayObject = [{one: "1", two: "2", five: "5"}, 
               {one: "1", two: "2",  five: "5"}];

What's the best way to do it ?

Robert Daraż
  • 323
  • 2
  • 19

4 Answers4

5

You could map only the wanted properties ba destructuring an getting a new object from the variables.

var array = [{ one: "1", two: "2", three: "3", four: "4", five: "5" }, { one: "1", two: "2", three: "3", four: "4", five: "5" }],
    result = array.map(({ one, two, five }) => ({ one, two, five }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

function extractObjectWithKeys(objects, keys) {
  return objects.reduce((acc, elem) => {
    let newObj = {};
    let filteredKeys = Object.keys(elem).filter(k => keys.includes(k));
    filteredKeys.forEach(key => {
      newObj[key] = elem[key];
    });
    return acc.concat(newObj);
  }, []);
}

const arrayObject = [
  {one: "1", two: "2", three: "3", four: "4", five: "5"}, 
  {one: "1", two: "2", three: "3", four: "4", five: "5"}
];

//Usage:
extractObjectWithKeys(arrayObject, ["one", "two", "five"])
Bakhtiiar Muzakparov
  • 2,308
  • 2
  • 15
  • 24
0

If you have an array of items you wish to keep you can use Object.fromEntries() with .map(). By mapping your array of items you want to keep (here I called it keep) to an array of key-value pairs (ie: [[key, value], ...]). You can then use Object.fromEntries() to turn the given array into an object like so:

const keep = ["one", "two", "five"];
const arrayObject = [{ one: "1", two: "2", three: "3", four: "4", five: "5" }, { one: "1", two: "2", three: "3", four: "4", five: "5" } ];

const res = arrayObject.map(
  obj => Object.fromEntries(keep.map(key => [key, obj[key]])
));
console.log(res);

Do note, Object.fromEntries() does have limited browser support, if you need something with a little better browser support you could use .reduce() like so:

const keep = ["one", "two", "five"];
const arrayObject = [{ one: "1", two: "2", three: "3", four: "4", five: "5" }, { one: "1", two: "2", three: "3", four: "4", five: "5" } ];
const res = arrayObject.map(
  obj => keep.map(key => [key, obj[key]]).reduce((acc, [k, v]) => ({...acc, [k]: v}), {})
);
console.log(res);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

You can map and use spread like below

arrayObject = [{one: "1", two: "2", three: "3", four: "4", five: "5"}, 
               {one: "1", two: "2", three: "3", four: "4", five: "5"}];
               
let filtered=arrayObject.map(a=>{

  const {three,four,...rest}=a;
  return rest;
});
console.log(filtered);
sumit
  • 15,003
  • 12
  • 69
  • 110
  • [he](https://stackoverflow.com/users/2284676/octohedron) knows why (his answer here was downvoted and then deleted) – Aksen P Dec 12 '19 at 11:51