将对象数组转换为其子数组后的新数组
transform array of object into new array following with its child
我有一个这样的数组
var data = [
{
name: "Movies", info: "category_name",
content: [
{ name: "Interstellar", info: "category_data" },
{ name: "Dark Knight", info: "category_data" },
]
},
{
name: "Music", info: "category_name",
content: [
{ name: "Adams", info: "category_data" },
{ name: "Nirvana", info: "category_data" },
]
},
{
name: "Places", info: "category_name",
content: [
{ name: "Jordan", info: "category_data" },
{ name: "Punjab", info: "category_data" },
]
},
]
想改造成这样
var transformedArray= [
{ name: "Movies", info: "category_name" },
{ name: "Interstellar", info: "category_data" },
{ name: "Dark Knight", info: "category_data" },
{ name: "Music", info: "category_name" },
{ name: "Adams", info: "category_data" },
{ name: "Nirvana", info: "category_data" },
{ name: "Places", info: "category_name" },
{ name: "Jordan", info: "category_data" },
{ name: "Punjab", info: "category_data" },
]
我不知道适合这种情况的关键字是什么,
我试过将它映射到新数组,但它与我预期的不一样
var newArr = []
var manipulate = data.map(item => {
return (
newArr.push(item),
new1.map(items => {
return (
new1.push(items)
)
})
)
})
那么如何将"data"操作成"transformedArray"
您可以使用 reduce
方法和扩展语法 ...
。
var data = [{"name":"Movies","info":"category_name","content":[{"name":"Interstellar","info":"category_data"},{"name":"Dark Knight","info":"category_data"}]},{"name":"Music","info":"category_name","content":[{"name":"Adams","info":"category_data"},{"name":"Nirvana","info":"category_data"}]},{"name":"Places","info":"category_name","content":[{"name":"Jordan","info":"category_data"},{"name":"Punjab","info":"category_data"}]}]
const result = data.reduce((r, {content, ...rest}) => {
r.push({...rest}, ...content)
return r;
}, []);
console.log(result);
您也可以使用 concat
和 reduce
代替扩展语法。
var data = [{"name":"Movies","info":"category_name","content":[{"name":"Interstellar","info":"category_data"},{"name":"Dark Knight","info":"category_data"}]},{"name":"Music","info":"category_name","content":[{"name":"Adams","info":"category_data"},{"name":"Nirvana","info":"category_data"}]},{"name":"Places","info":"category_name","content":[{"name":"Jordan","info":"category_data"},{"name":"Punjab","info":"category_data"}]}]
const result = data.reduce((r, {content, ...rest}) => r.concat(rest, content), []);
console.log(result);
使用Array.prototype.flatMap()
. If your browser or version of Node.js does not yet natively support this function, you can include a simple polyfill below, based on the specification:
if (!Array.prototype.flatMap) {
Object.defineProperty(Array.prototype, 'flatMap', {
configurable: true,
writable: true,
value: function flatMap (callback, thisArg = undefined) {
return this.reduce((array, ...args) => {
const element = callback.apply(thisArg, args);
if (Array.isArray(element)) array.push(...element);
else array.push(element);
return array;
}, []);
}
});
}
const data = [{name:'Movies',info:'category_name',content:[{name:'Interstellar',info:'category_data'},{name:'Dark Knight',info:'category_data'}]},{name:'Music',info:'category_name',content:[{name:'Adams',info:'category_data'},{name:'Nirvana',info:'category_data'}]},{name:'Places',info:'category_name',content:[{name:'Jordan',info:'category_data'},{name:'Punjab',info:'category_data'}]}];
const transformedArray = data.flatMap(({ content, ...o }) => [o, ...content]);
console.log(transformedArray);
我有一个这样的数组
var data = [
{
name: "Movies", info: "category_name",
content: [
{ name: "Interstellar", info: "category_data" },
{ name: "Dark Knight", info: "category_data" },
]
},
{
name: "Music", info: "category_name",
content: [
{ name: "Adams", info: "category_data" },
{ name: "Nirvana", info: "category_data" },
]
},
{
name: "Places", info: "category_name",
content: [
{ name: "Jordan", info: "category_data" },
{ name: "Punjab", info: "category_data" },
]
},
]
想改造成这样
var transformedArray= [
{ name: "Movies", info: "category_name" },
{ name: "Interstellar", info: "category_data" },
{ name: "Dark Knight", info: "category_data" },
{ name: "Music", info: "category_name" },
{ name: "Adams", info: "category_data" },
{ name: "Nirvana", info: "category_data" },
{ name: "Places", info: "category_name" },
{ name: "Jordan", info: "category_data" },
{ name: "Punjab", info: "category_data" },
]
我不知道适合这种情况的关键字是什么, 我试过将它映射到新数组,但它与我预期的不一样
var newArr = []
var manipulate = data.map(item => {
return (
newArr.push(item),
new1.map(items => {
return (
new1.push(items)
)
})
)
})
那么如何将"data"操作成"transformedArray"
您可以使用 reduce
方法和扩展语法 ...
。
var data = [{"name":"Movies","info":"category_name","content":[{"name":"Interstellar","info":"category_data"},{"name":"Dark Knight","info":"category_data"}]},{"name":"Music","info":"category_name","content":[{"name":"Adams","info":"category_data"},{"name":"Nirvana","info":"category_data"}]},{"name":"Places","info":"category_name","content":[{"name":"Jordan","info":"category_data"},{"name":"Punjab","info":"category_data"}]}]
const result = data.reduce((r, {content, ...rest}) => {
r.push({...rest}, ...content)
return r;
}, []);
console.log(result);
您也可以使用 concat
和 reduce
代替扩展语法。
var data = [{"name":"Movies","info":"category_name","content":[{"name":"Interstellar","info":"category_data"},{"name":"Dark Knight","info":"category_data"}]},{"name":"Music","info":"category_name","content":[{"name":"Adams","info":"category_data"},{"name":"Nirvana","info":"category_data"}]},{"name":"Places","info":"category_name","content":[{"name":"Jordan","info":"category_data"},{"name":"Punjab","info":"category_data"}]}]
const result = data.reduce((r, {content, ...rest}) => r.concat(rest, content), []);
console.log(result);
使用Array.prototype.flatMap()
. If your browser or version of Node.js does not yet natively support this function, you can include a simple polyfill below, based on the specification:
if (!Array.prototype.flatMap) {
Object.defineProperty(Array.prototype, 'flatMap', {
configurable: true,
writable: true,
value: function flatMap (callback, thisArg = undefined) {
return this.reduce((array, ...args) => {
const element = callback.apply(thisArg, args);
if (Array.isArray(element)) array.push(...element);
else array.push(element);
return array;
}, []);
}
});
}
const data = [{name:'Movies',info:'category_name',content:[{name:'Interstellar',info:'category_data'},{name:'Dark Knight',info:'category_data'}]},{name:'Music',info:'category_name',content:[{name:'Adams',info:'category_data'},{name:'Nirvana',info:'category_data'}]},{name:'Places',info:'category_name',content:[{name:'Jordan',info:'category_data'},{name:'Punjab',info:'category_data'}]}];
const transformedArray = data.flatMap(({ content, ...o }) => [o, ...content]);
console.log(transformedArray);