Say I have the array
const idArray = ["935", "933", "930"];
And I would like to get the objects that have an id property that matches one of the values in the idArray
const objectsArray= [
{
name: "Kevin",
color: "red",
id: "935"
},
{
name: "Ana",
color: "white",
id: "815"
},
{
name: "Maria",
color: "silver",
id: "035"
},
{
name: "Victor",
color: "red",
id: "935"
},
{
name: "Vanessa",
color: "red",
id: "933"
},
]
So in this case, i would like to return the objects that have the names: Kevin, Vanessa and Victor.
asked Apr 17, 2018 at 17:29
vanegeek
7132 gold badges10 silver badges23 bronze badges
-
2So, where are you stuck? :-) Do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Good luck!T.J. Crowder– T.J. Crowder2018年04月17日 17:30:14 +00:00Commented Apr 17, 2018 at 17:30
-
Your data structure not allow to solve the problem efficiently, i would suggest you to change (if possible) the array of objects to and object that the key will be the id, and the value will be an object with name & colorfelixmosh– felixmosh2018年04月17日 17:33:11 +00:00Commented Apr 17, 2018 at 17:33
-
3@felixmosh Turning it into hash table would make for faster queries for sure, but I honestly doubt that this information is somewhat useful to OP if he/she struggles with the posted problem.Matus Dubrava– Matus Dubrava2018年04月17日 17:37:09 +00:00Commented Apr 17, 2018 at 17:37
3 Answers 3
You can filter based on the idArray. This could potentially be slow if both arrays are large since it needs to look through idArray each time:
const objectsArray= [{ name: "Kevin", color: "red", id: "935"},{ name: "Ana", color: "white", id: "815"},{ name: "Maria", color: "silver", id: "035"},{ name: "Victor", color: "red", id: "935"},{ name: "Vanessa", color: "red", id: "933"},]
const idArray = ["935", "933", "930"];
let res = objectsArray.filter(o => idArray.includes(o.id))
console.log(res)
answered Apr 17, 2018 at 17:35
Mark
92.7k8 gold badges116 silver badges156 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Mark
@MatusDubrava you could vote to close the question if you like.
Elroy Jetson
@MatusDubrava the reason Google is helpful is because of answers to questions like this
yes actually it is very easy to achieve using filter.
const idArray = ["935", "933", "930"];
const objectsArray = [
{
name: "Kevin",
color: "red",
id: "935"
},
{
name: "Ana",
color: "white",
id: "815"
},
{
name: "Maria",
color: "silver",
id: "035"
},
{
name: "Victor",
color: "red",
id: "935"
},
{
name: "Vanessa",
color: "red",
id: "933"
},
];
function getObjectsInArray(objectsArray, idsArray){
return objectsArray.filter(o=>{
return idsArray.indexOf(o.id) !== -1;
})
}
console.log(getObjectsInArray(objectsArray, idArray))
answered Apr 17, 2018 at 17:37
Prince Hernandez
3,7411 gold badge13 silver badges20 bronze badges
Comments
const objectsArray= [{ name: "Kevin", color: "red", id: "935"},{ name: "Ana", color: "white", id: "815"},{ name: "Maria", color: "silver", id: "035"},{ name: "Victor", color: "red", id: "935"},{ name: "Vanessa", color: "red", id: "933"},]
console.log(find(objectsArray));
function find(objArray){
const idArray = ["935", "933", "930"];
var results = [];
for(var i = 0; i < objArray.length; i++){
if(idArray.includes(objArray[i].id)){
results.push(objArray[i]);
}
}
return results;
}
answered Apr 17, 2018 at 17:39
Elroy Jetson
9761 gold badge13 silver badges28 bronze badges
Comments
lang-js