0

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.

3
  • 2
    So, 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! Commented 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 & color Commented 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. Commented Apr 17, 2018 at 17:37

3 Answers 3

4

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)

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

2 Comments

@MatusDubrava you could vote to close the question if you like.
@MatusDubrava the reason Google is helpful is because of answers to questions like this
0

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

Comments

0

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

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.