0

I am pretty sure about this kind of question answered before, but I couldn't make good search.

I have an array of object like;

[
  { prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1"}
  { prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2"}
  { prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"}
]

And I have an array which is storing needed property names like;

[ "prop1", "prop2" ]

So I need to filter all of my objects in array with the property names which is given by another array. And the output will be like;

[   
 { prop1:"foo1", prop2:"baz1" }   
 { prop1:"foo2", prop2:"baz2" }  
 { prop1:"foo3", prop2:"baz3" } 
]

How can I do this in proper way ?

4 Answers 4

4

You can use map() and reduce()

const data = [
  { prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1"},
  { prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2"},
  { prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"}
];

const props = [ "prop1", "prop2" ];

const res = data.map(e => props.reduce((a,c) => (a[c] = e[c] , a), {}));

console.log(res)

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

3 Comments

Nice solution, could you please use more explicit variable names for the sake of readability? I understand the point of having a one liner, but reduce's params are obscure enough (upvoting anyway)
@axelduch a is accumulator, c is current element. Docs outline what arguments are
I was rather saying that for those unfamiliar with reduce. It doesn't have to be scary to them :) But fair enough - people curious enough about it will check the doc anyway
4

You can create pick function with reduce method and then use it with map method.

const data = [{ prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1"},{ prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2"},{ prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"}]
const props = [ "prop1", "prop2" ]

const pick = (o, p) => p.reduce((r, e) => Object.assign(r, {[e]: o[e]}), {})
const res = data.map(o => pick(o, props));
console.log(res)

Comments

3

You can do something like below to achieve the result:

const data = [
  { prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1"},
  { prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2"},
  { prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"},
]

const keys = [ "prop1", "prop2" ]

let result = data.map((record) => {
  let obj = {}
  keys.forEach((key) => {
    obj[key] = record[key]
  })
  return obj
})

console.log(result)

Comments

1

let arr = [
  { prop1:"foo1", prop2:"baz1", prop3:"bar1", prop4:"qux1"},
  { prop1:"foo2", prop2:"baz2", prop3:"bar2", prop4:"qux2"},
  { prop1:"foo3", prop2:"baz3", prop3:"bar3", prop4:"qux3"}
]

let filter = [ "prop1", "prop2" ];

let out = [...arr].map(e => Object.keys(e).map(k => !filter.includes(k) ? delete e[k] :true) && e);
console.log(out)

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.