gpt4 book ai didi

Javascript 将平面数据转换为嵌套对象

转载 作者:行者123 更新时间:2023-12-01 01:26:13 26 4
gpt4 key购买 nike

我对react/ES6还很陌生,当我使用reduce方法将从API接收到的平面数组转换为嵌套对象时,我遇到了奇怪的行为。我已经熟悉了map和filter,但之前从未使用过reduce。

我很确定我使用错误,但它是这样的。

我有以下平面数据集:

[
{
category:"Category1",
subCategory:"SubCategory1",
productType:"ProductType1",
brand:"brand1"
},
{
category:"Category1",
subCategory:"SubCategory2",
productType:"ProductType2",
brand:"brand2"
},
{
category:"Category1",
subCategory:"SubCategory2",
productType:"ProductType2",
brand:"brand3"
},
{
category:"Category2",
subCategory:"SubCategory1",
productType:"ProductType1",
brand:"brand1"
},
{
category:"Category2",
subCategory:"SubCategory1",
productType:"ProductType2",
brand:"brand2"
}
]

我使用以下代码从平面数据集中获取嵌套对象。

getNestedData() {
var baseData = this.getBaseData();

return baseData.reduce((categories,category) => {
if(!categories[category.category]){
categories[category.category] = {
category: category.category,
subCategories: baseData.filter(bd => bd.category === category.category)
.reduce((subCategories,subCategory) => {
if(!subCategories[subCategory.subCategory]) {
subCategories[subCategory.subCategory] = {
subCategory: subCategory.subCategory,
productTypes: baseData.filter(bd => bd.category === subCategory.category && bd.subCategory === subCategory.subCategory)
.reduce((productTypes,productType) => {
if(!productTypes[productType.productType]) {
productTypes[productType.productType] = {
productType: productType.productType,
brands: baseData.filter(bd => bd.category === productType.category && bd.subCategory === productType.subCategory && bd.productType === productType.productType)
.reduce((brands,brand) => {
if(!brands[brand.brand]) {
brands[brand.brand] = {
brand: brand.brand
}
}
return brands;
})
};
}
return productTypes;
})
};
}
return subCategories;
})
}
}
return categories;
});
}

但是,结果并不是我所期望的。它返回数组第一个类别内的根数组对象。就像Category1 => [Category1,Category2]。如果您进入类别 1 子项,也会发生同样的情况。

我准备了以下jsFiddle显示我的问题。

让我知道我做错了什么,我在谷歌上搜索了几个小时。我也想知道这种方式是不是最有效的方式?

编辑:

有一个预期输出的示例:

[
{
category: "Category1"
subCategories:[
{
subCategory:"SubCategory1",
productTypes:[
{
productType:"ProductType1",
brands:["Brand1"]
}
]
},
{
subCategory:"SubCategory2",
productTypes:[
{
productType:"ProductType2",
brands:["Brand2","Brand3"]
}
]
}
]
},
{
category: "Category2",
subCategories:[
{
subCategory:"SubCategory1",
productTypes:[
{
productType:"ProductType1",
brands:["Brand1"]
},
{
productType:"ProductType2",
brands:["Brand2"]
}
]
}
]
}
]

编辑

Mat 是对的,我在使用reduce 时忘记传递初始值。正如我预期的那样,传递初始值可以减少工作量。然后我就能够更改我的代码以获得预期的结果。该代码不是最优雅的,但效果很好:

getNestedData()
{
var baseData = this.getBaseData();
return baseData
.map(c => {return c.category;})
.reduce((categories,category) => {
var currentCategory = categories.filter(
c => c.category === category
);
if (currentCategory.length === 0) {
var categoryData = baseData.filter(bd => bd.category === category);
categories.push({
category:category,
subCategories: categoryData
.map(c => {return c.subCategory})
.reduce((subCategories,subCategory) => {
var currentSubCategory = subCategories.filter(sc => sc.subCategory === subCategory)
if(currentSubCategory.length === 0)
{
var subCategoryData = categoryData.filter(cd => cd.subCategory === subCategory);
subCategories.push({
subCategory:subCategory,
productTypes: subCategoryData
.map(scd => {return scd.productType})
.reduce((productTypes,productType) => {
var currentproductType = productTypes.filter(pt => pt.productType === productType);
if(currentproductType.length === 0)
{
var productTypeData = subCategoryData.filter(scd => scd.productType === productType)
productTypes.push({
productType:productType,
brands: productTypeData
.map(ptd => {return ptd.brand})
.reduce((brands,brand) => {
var currentBrand = brands.filter(b => b.brand === brand);
if(currentBrand.length === 0)
{
brands.push({brand:brand});
}
return brands;
},[])
});
}
return productTypes;
},[])
});
}
return subCategories;
},[])
});
}
return categories;
},[])
}

There是具有预期行为的工作 jsFiddle。

最佳答案

所以看起来您正在尝试对数组数据进行非规范化。你的重度嵌套函数很难阅读,我认为如果你使用 lodash 的 set ,你可以更轻松地完成你想要的事情。功能。例如:

const data = [
{
category:"Category1",
subCategory:"SubCategory1",
productType:"ProductType1",
brand:"brand1"
},
{
category:"Category1",
subCategory:"SubCategory2",
productType:"ProductType2",
brand:"brand2"
},
{
category:"Category1",
subCategory:"SubCategory2",
productType:"ProductType2",
brand:"brand3"
},
{
category:"Category2",
subCategory:"SubCategory1",
productType:"ProductType1",
brand:"brand1"
},
{
category:"Category2",
subCategory:"SubCategory1",
productType:"ProductType2",
brand:"brand2"
}
]

const result = data.reduce((output, item) => {
_.set(output, `${item.category}.subCategories.${item.subCategory}.productTypes.${item.productType}.brands.${item.brand}.brand`, item.brand)
return output
}, {})

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

您的代码无法正常工作的原因(忽略拼写错误)是因为我认为您忘记了为 reduce 函数提供初始值。查看最后一个可选参数 here .

关于Javascript 将平面数据转换为嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53754178/

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