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"

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Pontios
  • 2,377
  • 27
  • 32

11 Answers11

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);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

You can do it this way:

var objectArray = [];
myList.forEach(function(key){
    if(myObj[key]){ 
       objectArray.push(myObj[key]);
     }
});
Adam Neuwirth
  • 529
  • 5
  • 10
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)
Evgeny Klimenchenko
  • 1,184
  • 1
  • 6
  • 15
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];
        }
    })
Qodeer
  • 166
  • 7
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);
epascarello
  • 204,599
  • 20
  • 195
  • 236
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% }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

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

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

cDitch
  • 374
  • 7
  • 10
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)
Thales
  • 31
  • 4
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);
Jens Ingels
  • 806
  • 1
  • 9
  • 12
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)
Sven.hig
  • 4,449
  • 2
  • 8
  • 18
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'}]
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
8HoLoN
  • 1,122
  • 5
  • 14