1

I have an object like this:

myObj = { 1:{name:'a'}, 2:{name:'v'}, 3:{name:'x'}, 10:{name:'t'} };

and I want to get a list of the values whose keys are in the following list:

myList = [1,2,10]

So the result I want is:

[{name:'a'},{name:'v'}, {name:'t'}]

Any ideas on how this can be done?

I tried the following:

const result = [];
        
for (const childId in state.frameObjects[id].childrenIds ) 
{
     if(state.frameObjects[childId]!==undefined)
     {
          result.push(state.frameObjects[childId]);
     }
}
        
return result;

but Vue console gives me this error : InternalError: "too much recursion"

0

11 Answers 11

1

You can put the keys you want into a Set and use filter. This will also work if the array contains keys that are not present in the object.

const myObj = { 1:{name:'a'}, 2:{name:'v'}, 3:{name:'x'}, 10:{name:'t'} };
const keys = new Set([1,2,10])
const res = Object.keys(myObj).filter(key=>keys.has(+key)).map(key=>myObj[key]);
console.log(res);

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

Comments

0

You can do it this way:

var objectArray = [];
myList.forEach(function(key){
    if(myObj[key]){ 
       objectArray.push(myObj[key]);
     }
});

Comments

0

You can just iterate over you array with keys and just check if they exist in your myObj if they are then you can just push the value into the new arrya:

const myObj = { 1:{name:'a'}, 2:{name:'v'}, 3:{name:'x'}, 10:{name:'t'} };
const myList = [1,2,10]
const newArr = []
myList.forEach(key => {
  if (myObj[key]) {
    newArr.push(myObj[key])
  }
})


console.log(newArr)

Comments

0

You could filter they object's keys and then map the result.

const valuesOf = (obj, keys) => Object.keys(obj)
  .filter(key => keys.includes(parseInt(key, 10)))
  .map(key => obj[key])

const myObj = { 1: { name: 'a'}, 2: { name: 'v'}, 3: { name: 'x'}, 10: { name: 't'} };

console.log(valuesOf(myObj, [1, 2, 10]))
.as-console-wrapper { top: 0; min-height: 100% }

Or, you could attempt to map and filter later. This allows you to return the objects in the order of the desired keys.

const valuesOf = (obj, keys) =>
  keys.map(key => obj[key]).filter(v => v !== undefined)

const myObj = { 1: { name: 'a'}, 2: { name: 'v'}, 3: { name: 'x'}, 10: { name: 't'} };

console.log(valuesOf(myObj, [1, 2, 10]))
.as-console-wrapper { top: 0; min-height: 100% }

Comments

0

A more functional approach would be: myList.map(a => myObj[a]).filter(a => a)

Note: .filter(a => a) will remove undefined

Comments

0

This can be done several ways; One of the options is to loop through the object properties

const list = [];      
for(let prop in myObj){
       if(myList.includes(prop)){
           list.push(myObj[prop])
        }
    }

Same result can be achieved using Object.keys()

 const result = Object.keys(myObj).map((x) => {
        if (myList.includes(parseInt(x))) {
           return myObj[x];
        }
    })

Comments

0

try this

let myObj = { 1:{name:'a'}, 2:{name:'v'}, 3:{name:'x'}, 10:{name:'t'} };
    let objsArray = []
    let myList = [1,2,10];
    for (let index = 0; index < myList.length; index++) {
       const element = myList[index];
       objsArray.push(myObj[element])
  
    }
    
    console.log(objsArray)

Comments

0

Just need map

var myObj = { 1:{name:'a'}, 2:{name:'v'}, 3:{name:'x'}, 10:{name:'t'} };
var myList = [1,2,10]
// var result = myList.map( function(k){ return myObj[k]; })
var result = myList.map( k => myObj[k])

console.log(result);

and if people said it might not be in the list, use filter

var myObj = { 1:{name:'a'}, 2:{name:'v'}, 3:{name:'x'}, 10:{name:'t'} };
var myList = [1,2,10, 33, 44, 555]
// var result = myList.map( function(k){ return myObj[k]; }).filter(function(o) { return o; });
var result = myList.map( k => myObj[k]).filter(Boolean);

console.log(result);

or use reduce for one loop

var myObj = { 1:{name:'a'}, 2:{name:'v'}, 3:{name:'x'}, 10:{name:'t'} };
var myList = [1,2,10, 33, 44, 555]
var result = myList.reduce((a, k) =>
  (myObj[k] && a.push(myObj[k]), a), [])
// var result = myList.reduce(function(a, k) {
//  if(myObj[k]) { 
//     a.push(myObj[k]);
//   }
//   return a
// }, []);
console.log(result);

3 Comments

It will cause undefineds in the array if the key is not presented
As previously mentioned on this answer, this won't work in all cases.
so run filter if that is the case....
0

Using Map

 myObj = { 1:{name:'a'}, 2:{name:'v'}, 3:{name:'x'}, 10:{name:'t'} };
 myList = [1,2,10]
 map= new Map(),res=[]
 Object.entries(myObj).map(o=>map.set(o[0],o[1]))
 myList.forEach(o=>{ v=map.get(o.toString())
   if(v) res.push(v)})
 console.log(res)

Comments

0

const myObj = { 1:{name:'a'}, 2:{name:'v'}, 3:{name:'x'}, 10:{name:'t'} };
const myList = [1,2,10];

const result = myList?.map(key => (myObj == null ? null : myObj[key]), []) || [];
console.log(result);

Comments

0

Here is what you want:

const myObj = { 1:{name:'a'}, 2:{name:'v'}, 3:{name:'x'}, 10:{name:'t'} };

const myList = [1,2,10];

const res = Object.entries(myObj).filter(([k,v])=>{
  return myList.includes(+k);
}).map(([k,v])=>{
  return v;
})
console.log(res);//[{name:'a'},{name:'v'}, {name:'t'}]

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.