-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 ?

4
  • Are you wanting to get objects that only have them 3 keys or change the objects to just have them 3 keys? Commented Dec 12, 2019 at 11:32
  • Are you wanting to get an array object as per the key bases or value bases? Commented Dec 12, 2019 at 11:35
  • I want to change the arrya to have 3 keys. Commented Dec 12, 2019 at 11:35
  • arrayObject.map(({ three, four, ...rest }) => rest) Commented Dec 12, 2019 at 12:10

4 Answers 4

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

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

Comments

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"])

Comments

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

1 Comment

he knows why (his answer here was downvoted and then deleted)
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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.