2

I have the code below which I expect to map the result from the nested array and return a single array having both id's but I get 2 arrays instead. Can someone please guide me on what I'm doing wrongly?

arrayVal = [{
    sources: {
      data: [{
        id: 1
      }]
    }
  },
  {
    sources: {
      data: [{
        id: 2
      }]
    }
  }
]

for (let sub of arrayVal) {
  let result = sub.sources.data.map(x => (x.id))
  console.log(result)
}

3
  • 2
    "I get 2 arrays instead" No you don't, you get a single array twice. Commented May 23, 2018 at 4:37
  • 1
    This may help: Merge/flatten an array of arrays in JavaScript? Commented May 23, 2018 at 4:38
  • Thanks @JonathanLonowski Commented May 23, 2018 at 4:41

5 Answers 5

4

Right now, you're calling map for each element in arrayVal, so you get two arrays. Use reduce instead, to transform an array of objects into another array that's not necessarily one-to-one with the input elements:

const arrayVal=[{sources:{data:[{id:1}]}},{sources:{data:[{id:2}]}}];

const result = arrayVal.reduce((a, { sources: { data } }) => (
  [...a, ...data.map(({ id }) => id)]
), []);
console.log(result)

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

Comments

3

Try following

var arrayVal = [{sources: {data: [{id: 1}]}},{sources: {data: [{id: 2}]}}];

// Create an array on sources.data and merge it into 1 collection (array)
var result = arrayVal.reduce((a, c) => [...a, ...c.sources.data.map(({id}) => id)], []);

console.log(result);

For reference, Array.reduce

Also, you can improve your code as follows

var arrayVal = [{sources: {data: [{id: 1}]}},{sources: {data: [{id: 2}]}}];

let result = [];
for (let sub of arrayVal) {
  result.push(sub.sources.data.map(x => (x.id)));
}
console.log([].concat(...result))

Comments

3

arrayVal = [{
    sources: {
      data: [{
        id: 1
      }]
    }
  },
  {
    sources: {
      data: [{
        id: 2
      }]
    }
  }
]

let result = [];
for (let sub of arrayVal) {
  result = result.concat(sub.sources.data.map(x => (x.id)))

}

console.log(result)

I think concat is what you were missing here, Hope this is what you trying to achieve

Comments

1

Try this

arrayVal = [{
    sources: {
      data: [{
        id: 1
      }]
    }
  },
  {
    sources: {
      data: [{
        id: 2
      }]
    }
  }
]

let result = arrayVal.map((x) => x.sources.data[0].id)
console.log(result)

1 Comment

This does not work if data has more than one element.
1

You can do something like this:

arrayVal = [{
    sources: {
      data: [{
        id: 1
      }]
    }
  },
  {
    sources: {
      data: [{
        id: 2
      }]
    }
  }
]

var flat = arrayVal.reduce(function(prev,curr,cI){
  prev.push(curr.sources.data.map(x => (x.id))[0]);
  return prev; // *********  Important ******
}, []);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.