在对象数组中连接数组
Concat arrays in array of objects
我构建了这个数组:
const array = [
{
title: 'something',
list: ['a', 'b', 'c', 'd']
},
{
title: 'dog',
list: ['aa']
},
{
title: 'cat',
list: ['aaa', 'b', 'cccc']
},
{
title: 'apple',
list: [],
}
]
我想要一个包含其他数组中所有值的数组,所以:
const res = ['a', 'b', 'c', 'd', 'aa', 'aaa', 'b', 'cccc']
我可以吗?我可以使用 concat
但如何使用?
同时使用 concat
和 reduce
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),[]))
您可以减少数组以展平 属性。
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);
或即将到来的 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);
使用 reduce
和 ...
(展开运算符)非常简单。
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);
您可以在具有 map
的空数组上使用 concat
并扩展语法 ...
.
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)
忘记 flatmap
和 reduce
。让事情简单易懂。
var res = []
for (var i = 0; i < array.length; i++) {
res = res.concat(array[i]["list"])
}
我构建了这个数组:
const array = [
{
title: 'something',
list: ['a', 'b', 'c', 'd']
},
{
title: 'dog',
list: ['aa']
},
{
title: 'cat',
list: ['aaa', 'b', 'cccc']
},
{
title: 'apple',
list: [],
}
]
我想要一个包含其他数组中所有值的数组,所以:
const res = ['a', 'b', 'c', 'd', 'aa', 'aaa', 'b', 'cccc']
我可以吗?我可以使用 concat
但如何使用?
同时使用 concat
和 reduce
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),[]))
您可以减少数组以展平 属性。
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);
或即将到来的 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);
使用 reduce
和 ...
(展开运算符)非常简单。
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);
您可以在具有 map
的空数组上使用 concat
并扩展语法 ...
.
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)
忘记 flatmap
和 reduce
。让事情简单易懂。
var res = []
for (var i = 0; i < array.length; i++) {
res = res.concat(array[i]["list"])
}