2

I have the following array:

var k = {
    a: 67576567,
    b: 567657,
    c: "some",
    d: [
        {src:"b", id:1},
        {src:"c", id:2},
        {src:"d", id:3}
    ]
};

I'm looking for a way to obtain the following array without using a for:

["b", "c", "d"]

I tried to use the filter function but it always return the entire object:

var filtered = k.d.filter(function(value) {
    return value.src;
});

console.log(filtered);

Is there a way to do that?

lolol
  • 4,287
  • 3
  • 35
  • 54

1 Answers1

3

You just used the wrong function. What you need is Array.map

var arr = k.d.map(function(value) {
    return value.src;
});

Array.map is used to iterate and output a new array based on the return value of the callback while Array.filter just filters the existent array on the supposed boolean value that will be returned in the callback.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95