2

I build this array:

const array = [
  {
    title: 'something',
    list: ['a', 'b', 'c', 'd']
  },
  {
    title: 'dog',
    list: ['aa']
  },
  {
    title: 'cat',
    list: ['aaa', 'b', 'cccc']
  },
  {
    title: 'apple',
    list: [],
  }
]

I would like an array containing all the values in the other arrays, so:

const res = ['a', 'b', 'c', 'd', 'aa', 'aaa', 'b', 'cccc']

I can I do that? I can use concat but how?

5
  • What specifically are you having an issue figuring out? It seems fairly straight-forward. Commented Jan 31, 2019 at 15:31
  • 1
    array.reduce((result,item)=>result.concat(item.list),[]) Commented Jan 31, 2019 at 15:31
  • what is expected output Commented Jan 31, 2019 at 15:32
  • @MaheerAli The array shown in the question? Commented Jan 31, 2019 at 15:36
  • None of the answers mention it, but since you seem to be relatively new to JS, you could also just iterate and concat each object's list to an accumulator array. Commented Jan 31, 2019 at 15:37

5 Answers 5

3

You could reduce the array for flattening a property.

const
    array = [{ title: 'something', list: ['a', 'b', 'c', 'd'] }, { title: 'dog', list: ['aa'] }, { title: 'cat', list: ['aaa', 'b', 'cccc'] }, { title: 'apple', list: [] }],
    list = array.reduce((r, { list }) => [...r, ...list], []);

console.log(list);

Or take maybe upcoming flatMap.

const
    array = [{ title: 'something', list: ['a', 'b', 'c', 'd'] }, { title: 'dog', list: ['aa'] }, { title: 'cat', list: ['aaa', 'b', 'cccc'] }, { title: 'apple', list: [] }],
    list = array.flatMap(({ list }) => list);

console.log(list);

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

Comments

1

Use concat and reduce together

var a=[
  {
    title: 'something',
    list: ['a', 'b', 'c', 'd']
  },
  {
    title: 'dog',
    list: ['aa']
  },
  {
    title: 'cat',
    list: ['aaa', 'b', 'cccc']
  },
  {
    title: 'apple',
    list: [],
  }
];
console.log(a.reduce((acc,e)=>acc.concat(e.list),[]))

Comments

1

You could use concat on empty array with map and spread syntax ....

const array = [{"title":"something","list":["a","b","c","d"]},{"title":"dog","list":["aa"]},{"title":"cat","list":["aaa","b","cccc"]},{"title":"apple","list":[]}]

const res = [].concat(...array.map(({list}) => list))
console.log(res)

Comments

1

Forget flatmap and reduce. Keep things simple and easy to understand.

var res = []
for (var i = 0; i < array.length; i++) {
    res = res.concat(array[i]["list"])
}

Comments

0

using reduce and the ...(spread operator) it is very simple to do.

const array = [{
    title: 'something',
    list: ['a', 'b', 'c', 'd']
  },
  {
    title: 'dog',
    list: ['aa']
  },
  {
    title: 'cat',
    list: ['aaa', 'b', 'cccc']
  },
  {
    title: 'apple',
    list: [],
  }
]

const result = array.reduce((accum, item) => {
  return [...accum, ...item.list]
}, [])

console.log(result);

Comments

Your Answer

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