
在进行数据转换之前,我们首先需要明确输入数据的结构和期望输出的结构。
输入数据结构:
我们有两个主要的数组:boxes 和 items。
boxes 数组: 这是一个包含多个推车(trolley)信息的数组,每个推车对象内部又包含一个 trolleyItems 数组,表示推车中的具体物品。
const boxes = [
{
"trolleyNo": "345A",
"trolleyItems": [
{
"key": "02f2c8e0-cd40-11ed-8563-092a964acecc",
"name": "Bar Cart - Liquor, Liqueur",
"value": "Bar Cart - Liquor, Liqueur",
"label": "Bar Cart - Liquor, Liqueur"
},
// ... 更多物品
],
"cartType": "fullCart", // 购物车类型
"index": "FC-093"
},
// ... 更多推车
];其中,trolleyItems 数组中的每个对象都包含一个 name 字段,代表物品名称。
立即学习“Java免费学习笔记(深入)”;
items 数组: 这是一个包含所有可用物品详细信息的数组,每个物品对象都有一个唯一的 _id 和 name 字段。
const items = [
{
"_id": "646d96f669cad73dc5d14a25",
"name": "Bar Cart - Beer & Wine",
"customerId": "02f2c8e0-cd40-11ed-8563-092a964acecc"
},
// ... 更多物品
];这里的 _id 是我们最终需要提取的唯一标识符。
目标输出结构:
我们希望生成一个名为 final 的新数组,其结构如下:
const final = [
{
"cartId": "full", // 来自 boxes 数组中的 cartType
"itemsCategory": [ // 一个包含物品分类ID的数组
{
"categoryId": "646d96f669cad73dc5d14a24" // 匹配 trolleyItems.name 与 items.name 得到的 items._id
},
// ... 更多 categoryId
]
},
// ... 更多购物车
];核心转换逻辑在于:
重要提示: 请注意,您提供的目标 final 数组示例中的 categoryId 值(如 "645bbe9141332374a05919d2")与 items 数组中实际的 _id 值(如 "646d96f669cad73dc5d14a25")不匹配。在本教程中,我们将严格按照问题描述,从 items 数组中根据 name 匹配获取真实的 _id 值作为 categoryId。
JavaScript 的 Array.prototype.map() 和 Array.prototype.find() 方法非常适合这种嵌套数据的转换和查找任务。
外层 map 转换 boxes 数组: 我们首先对 boxes 数组进行 map 操作,为每个推车对象生成一个新的结构,包含 cartId 和一个待填充的 itemsCategory 数组。
内层 map 转换 trolleyItems: 在外部 map 的回调函数内部,我们对当前推车对象的 trolleyItems 数组再次进行 map 操作。这次 map 的目的是为每个 trolleyItem 生成一个 { categoryId: '...' } 结构。
使用 find 进行关联匹配: 在内层 map 的回调函数中,我们需要根据 trolleyItem.name 去 items 数组中查找对应的 item。Array.prototype.find() 方法可以帮助我们找到第一个满足条件的元素。一旦找到匹配的 item,我们就可以提取它的 _id 作为 categoryId。为了处理可能找不到匹配项的情况,我们使用可选链操作符 ?. 来安全地访问 _id。
下面是实现上述逻辑的完整代码示例:
const boxes = [
{
"trolleyNo": "345A",
"trolleyItems": [
{
"key": "02f2c8e0-cd40-11ed-8563-092a964acecc",
"name": "Bar Cart - Liquor, Liqueur",
"value": "Bar Cart - Liquor, Liqueur",
"label": "Bar Cart - Liquor, Liqueur"
},
{
"key": "02f2c8e0-cd40-11ed-8563-092a964acecc",
"name": "Bar Cart - Beer & Wine",
"value": "Bar Cart - Beer & Wine",
"label": "Bar Cart - Beer & Wine"
},
{
"key": "02f2c8e0-cd40-11ed-8563-092a964acecc",
"name": "Bread Basket & Tongs",
"value": "Bread Basket & Tongs",
"label": "Bread Basket & Tongs"
},
{
"key": "02f2c8e0-cd40-11ed-8563-092a964acecc",
"name": "Crew Store - Cookies, juices, etc",
"value": "Crew Store - Cookies, juices, etc",
"label": "Crew Store - Cookies, juices, etc"
}
],
"cartType": "fullCart",
"index": "FC-093"
},
{
"trolleyNo": "560S",
"trolleyItems": [
{
"key": "02f2c8e0-cd40-11ed-8563-092a964acecc",
"name": "Bar Cart - Beer & Wine",
"value": "Bar Cart - Beer & Wine",
"label": "Bar Cart - Beer & Wine"
},
{
"key": "02f2c8e0-cd40-11ed-8563-092a964acecc",
"name": "Bread Basket & Tongs",
"value": "Bread Basket & Tongs",
"label": "Bread Basket & Tongs"
},
{
"key": "02f2c8e0-cd40-11ed-8563-092a964acecc",
"name": "Crockery - Full Plates",
"value": "Crockery - Full Plates",
"label": "Crockery - Full Plates"
}
],
"cartType": "halfCart",
"index": "FC-093"
}
];
const items = [
{
"_id": "646d96f669cad73dc5d14a25",
"name": "Bar Cart - Beer & Wine",
"customerId": "02f2c8e0-cd40-11ed-8563-092a964acecc"
},
{
"_id": "646d96f669cad73dc5d14a24",
"name": "Bar Cart - Liquor, Liqueur",
"customerId": "02f2c8e0-cd40-11ed-8563-092a964acecc"
},
{
"_id": "646d96f669cad73dc5d14a2b",
"name": "Bread Basket & Tongs",
"customerId": "02f2c8e0-cd以上就是JavaScript:高效转换嵌套数组对象数据为指定结构的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号