gpt4 book ai didi

javascript - 将对象数组分类为新数组

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:57:12 24 4
gpt4 key购买 nike

我有一个数组,我需要将这个数组映射到它的子数组中。

var data = [{
name: 'Cars',
content: 'BMW',
value: 2000
},
{
name: 'Cars',
content: 'Fiat',
value: 542
},
{
name: 'Cars',
content: 'Mercedes',
value: 745
},
{
name: 'Cars',
content: 'Toyota',
value: 965
},
{
name: 'Cars',
content: 'Honda',
value: 754
}, {
name: 'Cars',
content: 'VW',
value: 123
},
{
name: 'Cars',
content: 'Ford',
value: 200
},
{
name: 'Fruits',
content: 'Apple',
value: 500
}, {
name: 'Fruits',
content: 'Orange',
value: 769
}, {
name: 'Fruits',
content: 'Banana',
value: 120
}, {
name: 'Fruits',
content: 'Strawberry',
value: 48
}, {
name: 'Fruits',
content: 'Mango',
value: 653
},
{
name: 'Colors',
content: 'Red',
value: 965
}, {
name: 'Colors',
content: 'Black',
value: 931
}, {
name: 'Colors',
content: 'BMW',
value: 423
}, {
name: 'Colors',
content: 'BMW',
value: 964
}, {
name: 'Colors',
content: 'BMW',
value: 436
}
]

我想要的最终结果一定是这样的:

[
{
name: "Children Array",
children: [
{
name: "Cars",
children: [
{
name: "BMW",
value: 2000
},
{
name: "Fiat",
value: 542
},
{
...
}
]
},
{
name: "Fruits",
children: [
{
name: 'Apple',
value: 500
},
{
name: 'Orange',
value: 769
},
{
...
}
]
},
{
name: "Colors",
children: [
{
name: 'Red',
value: 965
},
{
...
}
]
}
]
}
]

这是我做的。

var data = [{
name: 'Cars',
content: 'BMW',
value: 2000
},
{
name: 'Cars',
content: 'Fiat',
value: 542
},
{
name: 'Cars',
content: 'Mercedes',
value: 745
},
{
name: 'Cars',
content: 'Toyota',
value: 965
},
{
name: 'Cars',
content: 'Honda',
value: 754
}, {
name: 'Cars',
content: 'VW',
value: 123
},
{
name: 'Cars',
content: 'Ford',
value: 200
},
{
name: 'Fruits',
content: 'Apple',
value: 500
}, {
name: 'Fruits',
content: 'Orange',
value: 769
}, {
name: 'Fruits',
content: 'Banana',
value: 120
}, {
name: 'Fruits',
content: 'Strawberry',
value: 48
}, {
name: 'Fruits',
content: 'Mango',
value: 653
},
{
name: 'Colors',
content: 'Red',
value: 965
}, {
name: 'Colors',
content: 'Black',
value: 931
}, {
name: 'Colors',
content: 'BMW',
value: 423
}, {
name: 'Colors',
content: 'BMW',
value: 964
}, {
name: 'Colors',
content: 'BMW',
value: 436
}
]

const layer1 = _.uniqWith(_.map(data, (item) => {
return item.name
}), _.isEqual)

const newArray = [{
name: 'Children Array',
children: _.map(layer1, (item) => {
return {
name: item,
children: _.map(data, (item) => {
return {

}
})
}
})
}]

console.log(newArray)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

我不知道如何处理第二个子层。

请指教。

最佳答案

您可以对数据进行分组并为结果提供最终布局。

var data = [{ name: 'Cars', content: 'BMW', value: 2000 }, { name: 'Cars', content: 'Fiat', value: 542 }, { name: 'Cars', content: 'Mercedes', value: 745 }, { name: 'Cars', content: 'Toyota', value: 965 }, { name: 'Cars', content: 'Honda', value: 754 }, { name: 'Cars', content: 'VW', value: 123 }, { name: 'Cars', content: 'Ford', value: 200 }, { name: 'Fruits', content: 'Apple', value: 500 }, { name: 'Fruits', content: 'Orange', value: 769 }, { name: 'Fruits', content: 'Banana', value: 120 }, { name: 'Fruits', content: 'Strawberry', value: 48 }, { name: 'Fruits', content: 'Mango', value: 653 }, { name: 'Colors', content: 'Red', value: 965 }, { name: 'Colors', content: 'Black', value: 931 }, { name: 'Colors', content: 'BMW', value: 423 }, { name: 'Colors', content: 'BMW', value: 964 }, { name: 'Colors', content: 'BMW', value: 436 }],
result = _(data)
.groupBy('name')
.map((group, name) =>
({ name, children: _.map(group, ({ content: name, value }) => ({ name, value })) }))
.value(),
final = [{ name: "Children Array", children: result }];

console.log(final);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

关于javascript - 将对象数组分类为新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52955026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com