Get Object Properties By Predefined Property Names Array Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Detecting an undefined object propertyWhat is the most efficient way to deep clone an object in JavaScript?How can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Sort array of objects by string property valueHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Iterate through object propertiesFor-each over an array in JavaScript?
Trying to understand entropy as a novice in thermodynamics
Google .dev domain strangely redirects to https
How many time has Arya actually used Needle?
How to change the tick of the color bar legend to black
Why is std::move not [[nodiscard]] in C++20?
What does it mean that physics no longer uses mechanical models to describe phenomena?
I can't produce songs
Is there hard evidence that the grant peer review system performs significantly better than random?
The test team as an enemy of development? And how can this be avoided?
What would you call this weird metallic apparatus that allows you to lift people?
What is the chair depicted in Cesare Maccari's 1889 painting "Cicerone denuncia Catilina"?
What does 丫 mean? 丫是什么意思?
How does light 'choose' between wave and particle behaviour?
Are the endpoints of the domain of a function counted as critical points?
Is openssl rand command cryptographically secure?
Random body shuffle every night—can we still function?
Is CEO the "profession" with the most psychopaths?
Printing attributes of selection in ArcPy?
Flight departed from the gate 5 min before scheduled departure time. Refund options
Why complex landing gears are used instead of simple,reliability and light weight muscle wire or shape memory alloys?
Sally's older brother
Is it dangerous to install hacking tools on my private linux machine?
Is multiple magic items in one inherently imbalanced?
In musical terms, what properties are varied by the human voice to produce different words / syllables?
Get Object Properties By Predefined Property Names Array
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Detecting an undefined object propertyWhat is the most efficient way to deep clone an object in JavaScript?How can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Sort array of objects by string property valueHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Iterate through object propertiesFor-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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 ?
javascript
add a comment |
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 ?
javascript
add a comment |
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 ?
javascript
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 ?
javascript
javascript
asked Mar 22 at 11:21
berkanberkan
1401111
1401111
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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)
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
Mar 22 at 11:30
1
@axelducha
is accumulator,c
is current element. Docs outline what arguments are
– charlietfl
Mar 22 at 11:32
1
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
– axelduch
Mar 22 at 11:37
add a comment |
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)
add a comment |
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)
add a comment |
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)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55298528%2fget-object-properties-by-predefined-property-names-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
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
Mar 22 at 11:30
1
@axelducha
is accumulator,c
is current element. Docs outline what arguments are
– charlietfl
Mar 22 at 11:32
1
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
– axelduch
Mar 22 at 11:37
add a comment |
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)
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
Mar 22 at 11:30
1
@axelducha
is accumulator,c
is current element. Docs outline what arguments are
– charlietfl
Mar 22 at 11:32
1
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
– axelduch
Mar 22 at 11:37
add a comment |
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)
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)
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)
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)
edited Mar 22 at 11:33
answered Mar 22 at 11:28
charlietflcharlietfl
143k1391126
143k1391126
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
Mar 22 at 11:30
1
@axelducha
is accumulator,c
is current element. Docs outline what arguments are
– charlietfl
Mar 22 at 11:32
1
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
– axelduch
Mar 22 at 11:37
add a comment |
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
Mar 22 at 11:30
1
@axelducha
is accumulator,c
is current element. Docs outline what arguments are
– charlietfl
Mar 22 at 11:32
1
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
– axelduch
Mar 22 at 11:37
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
Mar 22 at 11:30
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
Mar 22 at 11:30
1
1
@axelduch
a
is accumulator, c
is current element. Docs outline what arguments are– charlietfl
Mar 22 at 11:32
@axelduch
a
is accumulator, c
is current element. Docs outline what arguments are– charlietfl
Mar 22 at 11:32
1
1
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
– axelduch
Mar 22 at 11:37
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
– axelduch
Mar 22 at 11:37
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
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)
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)
answered Mar 22 at 11:26
Nenad VracarNenad Vracar
73.8k126085
73.8k126085
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
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)
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)
answered Mar 22 at 11:27
User97User97
1,58211435
1,58211435
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
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)
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)
answered Mar 22 at 11:54
AZ_AZ_
942310
942310
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55298528%2fget-object-properties-by-predefined-property-names-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown