-2

if i have this collection:

var objArray = [
        { id: 0, name: 'Object 0', otherProp: '321' },
        { id: 1, name: 'O1', otherProp: '648' },
        { id: 2, name: 'Another Object', otherProp: '850' },
        { id: 3, name: 'Almost There', otherProp: '046' },
        { id: 4, name: 'Last Obj', otherProp: '984' }
    ];

how to get the array of id's values, example result: {0,1,2,3,4}

any idea?

i have been googleing it but i can not get the answer, mostly done by filtering.

1 Answers1

0

Try using reduce.

var objArray = [
        { id: 0, name: 'Object 0', otherProp: '321' },
        { id: 1, name: 'O1', otherProp: '648' },
        { id: 2, name: 'Another Object', otherProp: '850' },
        { id: 3, name: 'Almost There', otherProp: '046' },
        { id: 4, name: 'Last Obj', otherProp: '984' }
    ];
    
var res = objArray.reduce(function(out, inp) {
              out.push(inp['id']);
              return out;
          }, []);
    
    console.log(res)
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59