How to get an array of unique values of a specific key provided I only have an array of id's of that object...
I have a store set up for a list of candidate objects like this
const candidatesStore = [
{ id: 1, status: active}
{id:2, status: inactive}
{id: 3 status: failed}
{id: 4 status:inactive}
{id: 5 status:failed}
.
... and so on ]
I only have the list of id's in an array, ex: requiredArray = [2,3,4]
How to get a list of unique statuses by just using requiredArray
using ES6 in the most efficient way
The result should be [inactive, failed]
I preferably want to use reduce
to get unique value, and find
to get the list of candidates
from candidatesStore
javascript ecmascript-6
add a comment |
I have a store set up for a list of candidate objects like this
const candidatesStore = [
{ id: 1, status: active}
{id:2, status: inactive}
{id: 3 status: failed}
{id: 4 status:inactive}
{id: 5 status:failed}
.
... and so on ]
I only have the list of id's in an array, ex: requiredArray = [2,3,4]
How to get a list of unique statuses by just using requiredArray
using ES6 in the most efficient way
The result should be [inactive, failed]
I preferably want to use reduce
to get unique value, and find
to get the list of candidates
from candidatesStore
javascript ecmascript-6
What's going on with thesecandidate1 =
assignments in your array literal?
– Bergi
Nov 12 '18 at 19:59
How aboutArray.from(new Set(candidatesStore.filter(cand => requiredArray.includes(cand.id)).map(cand => cand.status)))
? I don't see howreduce
orfind
could be useful here.
– Bergi
Nov 12 '18 at 20:00
add a comment |
I have a store set up for a list of candidate objects like this
const candidatesStore = [
{ id: 1, status: active}
{id:2, status: inactive}
{id: 3 status: failed}
{id: 4 status:inactive}
{id: 5 status:failed}
.
... and so on ]
I only have the list of id's in an array, ex: requiredArray = [2,3,4]
How to get a list of unique statuses by just using requiredArray
using ES6 in the most efficient way
The result should be [inactive, failed]
I preferably want to use reduce
to get unique value, and find
to get the list of candidates
from candidatesStore
javascript ecmascript-6
I have a store set up for a list of candidate objects like this
const candidatesStore = [
{ id: 1, status: active}
{id:2, status: inactive}
{id: 3 status: failed}
{id: 4 status:inactive}
{id: 5 status:failed}
.
... and so on ]
I only have the list of id's in an array, ex: requiredArray = [2,3,4]
How to get a list of unique statuses by just using requiredArray
using ES6 in the most efficient way
The result should be [inactive, failed]
I preferably want to use reduce
to get unique value, and find
to get the list of candidates
from candidatesStore
javascript ecmascript-6
javascript ecmascript-6
edited Nov 13 '18 at 22:23
Anand Dharne
asked Nov 12 '18 at 19:53
Anand DharneAnand Dharne
62
62
What's going on with thesecandidate1 =
assignments in your array literal?
– Bergi
Nov 12 '18 at 19:59
How aboutArray.from(new Set(candidatesStore.filter(cand => requiredArray.includes(cand.id)).map(cand => cand.status)))
? I don't see howreduce
orfind
could be useful here.
– Bergi
Nov 12 '18 at 20:00
add a comment |
What's going on with thesecandidate1 =
assignments in your array literal?
– Bergi
Nov 12 '18 at 19:59
How aboutArray.from(new Set(candidatesStore.filter(cand => requiredArray.includes(cand.id)).map(cand => cand.status)))
? I don't see howreduce
orfind
could be useful here.
– Bergi
Nov 12 '18 at 20:00
What's going on with these
candidate1 =
assignments in your array literal?– Bergi
Nov 12 '18 at 19:59
What's going on with these
candidate1 =
assignments in your array literal?– Bergi
Nov 12 '18 at 19:59
How about
Array.from(new Set(candidatesStore.filter(cand => requiredArray.includes(cand.id)).map(cand => cand.status)))
? I don't see how reduce
or find
could be useful here.– Bergi
Nov 12 '18 at 20:00
How about
Array.from(new Set(candidatesStore.filter(cand => requiredArray.includes(cand.id)).map(cand => cand.status)))
? I don't see how reduce
or find
could be useful here.– Bergi
Nov 12 '18 at 20:00
add a comment |
2 Answers
2
active
oldest
votes
You can use "reduce" as well as "filter" for this.
Note - I have to change your input object little bit as "candidate1 = ..." was not making much sense here.
const candidates = [
{id: 1, status: 'active'}
,{id: 2, status: 'inactive'}
,{id: 3, status: 'failed'}
,{id: 4, status: 'inactive'}
,{id: 5, status: 'failed'}
]
const requiredArray = [2,3,4]
let result = [...candidates.filter(({ id }) => requiredArray.includes(id))
.reduce((s, d) => s.add(d.status) , new Set)
]
console.log(result)
FWIW, you can also skip the.map
step and do.reduce((s, d) => s.add(d.status) , new Set)
– Felix Kling
Nov 13 '18 at 1:44
Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
– Nitish Narang
Nov 13 '18 at 3:57
1
I'm glad to hear that and you are very welcome :)
– Felix Kling
Nov 13 '18 at 17:13
add a comment |
Try this.
const candidates = [
{id: 1, status: "active"},
{id: 2, status: "inactive"},
{id: 3, status: "failed"},
{id: 4, status: "inactive"},
{id: 5, status: "failed"},
];
let requiredArray = [2,3,4];
let newArray = candidates.filter(x=>requiredArray.includes(x.id))
console.log(newArray);
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%2f53269178%2fhow-to-get-an-array-of-unique-values-of-a-specific-key-provided-i-only-have-an-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use "reduce" as well as "filter" for this.
Note - I have to change your input object little bit as "candidate1 = ..." was not making much sense here.
const candidates = [
{id: 1, status: 'active'}
,{id: 2, status: 'inactive'}
,{id: 3, status: 'failed'}
,{id: 4, status: 'inactive'}
,{id: 5, status: 'failed'}
]
const requiredArray = [2,3,4]
let result = [...candidates.filter(({ id }) => requiredArray.includes(id))
.reduce((s, d) => s.add(d.status) , new Set)
]
console.log(result)
FWIW, you can also skip the.map
step and do.reduce((s, d) => s.add(d.status) , new Set)
– Felix Kling
Nov 13 '18 at 1:44
Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
– Nitish Narang
Nov 13 '18 at 3:57
1
I'm glad to hear that and you are very welcome :)
– Felix Kling
Nov 13 '18 at 17:13
add a comment |
You can use "reduce" as well as "filter" for this.
Note - I have to change your input object little bit as "candidate1 = ..." was not making much sense here.
const candidates = [
{id: 1, status: 'active'}
,{id: 2, status: 'inactive'}
,{id: 3, status: 'failed'}
,{id: 4, status: 'inactive'}
,{id: 5, status: 'failed'}
]
const requiredArray = [2,3,4]
let result = [...candidates.filter(({ id }) => requiredArray.includes(id))
.reduce((s, d) => s.add(d.status) , new Set)
]
console.log(result)
FWIW, you can also skip the.map
step and do.reduce((s, d) => s.add(d.status) , new Set)
– Felix Kling
Nov 13 '18 at 1:44
Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
– Nitish Narang
Nov 13 '18 at 3:57
1
I'm glad to hear that and you are very welcome :)
– Felix Kling
Nov 13 '18 at 17:13
add a comment |
You can use "reduce" as well as "filter" for this.
Note - I have to change your input object little bit as "candidate1 = ..." was not making much sense here.
const candidates = [
{id: 1, status: 'active'}
,{id: 2, status: 'inactive'}
,{id: 3, status: 'failed'}
,{id: 4, status: 'inactive'}
,{id: 5, status: 'failed'}
]
const requiredArray = [2,3,4]
let result = [...candidates.filter(({ id }) => requiredArray.includes(id))
.reduce((s, d) => s.add(d.status) , new Set)
]
console.log(result)
You can use "reduce" as well as "filter" for this.
Note - I have to change your input object little bit as "candidate1 = ..." was not making much sense here.
const candidates = [
{id: 1, status: 'active'}
,{id: 2, status: 'inactive'}
,{id: 3, status: 'failed'}
,{id: 4, status: 'inactive'}
,{id: 5, status: 'failed'}
]
const requiredArray = [2,3,4]
let result = [...candidates.filter(({ id }) => requiredArray.includes(id))
.reduce((s, d) => s.add(d.status) , new Set)
]
console.log(result)
const candidates = [
{id: 1, status: 'active'}
,{id: 2, status: 'inactive'}
,{id: 3, status: 'failed'}
,{id: 4, status: 'inactive'}
,{id: 5, status: 'failed'}
]
const requiredArray = [2,3,4]
let result = [...candidates.filter(({ id }) => requiredArray.includes(id))
.reduce((s, d) => s.add(d.status) , new Set)
]
console.log(result)
const candidates = [
{id: 1, status: 'active'}
,{id: 2, status: 'inactive'}
,{id: 3, status: 'failed'}
,{id: 4, status: 'inactive'}
,{id: 5, status: 'failed'}
]
const requiredArray = [2,3,4]
let result = [...candidates.filter(({ id }) => requiredArray.includes(id))
.reduce((s, d) => s.add(d.status) , new Set)
]
console.log(result)
edited Nov 13 '18 at 6:38
answered Nov 12 '18 at 20:01
Nitish NarangNitish Narang
2,948815
2,948815
FWIW, you can also skip the.map
step and do.reduce((s, d) => s.add(d.status) , new Set)
– Felix Kling
Nov 13 '18 at 1:44
Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
– Nitish Narang
Nov 13 '18 at 3:57
1
I'm glad to hear that and you are very welcome :)
– Felix Kling
Nov 13 '18 at 17:13
add a comment |
FWIW, you can also skip the.map
step and do.reduce((s, d) => s.add(d.status) , new Set)
– Felix Kling
Nov 13 '18 at 1:44
Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
– Nitish Narang
Nov 13 '18 at 3:57
1
I'm glad to hear that and you are very welcome :)
– Felix Kling
Nov 13 '18 at 17:13
FWIW, you can also skip the
.map
step and do .reduce((s, d) => s.add(d.status) , new Set)
– Felix Kling
Nov 13 '18 at 1:44
FWIW, you can also skip the
.map
step and do .reduce((s, d) => s.add(d.status) , new Set)
– Felix Kling
Nov 13 '18 at 1:44
Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
– Nitish Narang
Nov 13 '18 at 3:57
Yeah Correct. Thank you for pointing Sir!! BTW been long time follower of your detailed answers, which has helped me understand JS better. Want to take this chance to Thank you for spreading knowledge!! 😊
– Nitish Narang
Nov 13 '18 at 3:57
1
1
I'm glad to hear that and you are very welcome :)
– Felix Kling
Nov 13 '18 at 17:13
I'm glad to hear that and you are very welcome :)
– Felix Kling
Nov 13 '18 at 17:13
add a comment |
Try this.
const candidates = [
{id: 1, status: "active"},
{id: 2, status: "inactive"},
{id: 3, status: "failed"},
{id: 4, status: "inactive"},
{id: 5, status: "failed"},
];
let requiredArray = [2,3,4];
let newArray = candidates.filter(x=>requiredArray.includes(x.id))
console.log(newArray);
add a comment |
Try this.
const candidates = [
{id: 1, status: "active"},
{id: 2, status: "inactive"},
{id: 3, status: "failed"},
{id: 4, status: "inactive"},
{id: 5, status: "failed"},
];
let requiredArray = [2,3,4];
let newArray = candidates.filter(x=>requiredArray.includes(x.id))
console.log(newArray);
add a comment |
Try this.
const candidates = [
{id: 1, status: "active"},
{id: 2, status: "inactive"},
{id: 3, status: "failed"},
{id: 4, status: "inactive"},
{id: 5, status: "failed"},
];
let requiredArray = [2,3,4];
let newArray = candidates.filter(x=>requiredArray.includes(x.id))
console.log(newArray);
Try this.
const candidates = [
{id: 1, status: "active"},
{id: 2, status: "inactive"},
{id: 3, status: "failed"},
{id: 4, status: "inactive"},
{id: 5, status: "failed"},
];
let requiredArray = [2,3,4];
let newArray = candidates.filter(x=>requiredArray.includes(x.id))
console.log(newArray);
const candidates = [
{id: 1, status: "active"},
{id: 2, status: "inactive"},
{id: 3, status: "failed"},
{id: 4, status: "inactive"},
{id: 5, status: "failed"},
];
let requiredArray = [2,3,4];
let newArray = candidates.filter(x=>requiredArray.includes(x.id))
console.log(newArray);
const candidates = [
{id: 1, status: "active"},
{id: 2, status: "inactive"},
{id: 3, status: "failed"},
{id: 4, status: "inactive"},
{id: 5, status: "failed"},
];
let requiredArray = [2,3,4];
let newArray = candidates.filter(x=>requiredArray.includes(x.id))
console.log(newArray);
answered Nov 12 '18 at 20:04
eag845eag845
878611
878611
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53269178%2fhow-to-get-an-array-of-unique-values-of-a-specific-key-provided-i-only-have-an-a%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
What's going on with these
candidate1 =
assignments in your array literal?– Bergi
Nov 12 '18 at 19:59
How about
Array.from(new Set(candidatesStore.filter(cand => requiredArray.includes(cand.id)).map(cand => cand.status)))
? I don't see howreduce
orfind
could be useful here.– Bergi
Nov 12 '18 at 20:00