javascript - 循环遍历一个对象中的多个数组以在一个新数组中创建多个对象?
问题描述
在我的应用程序中,$scope.results = [{},{},{},{}.....]。(是一个包含多个对象的数组)
对象之一的示例输出:
0:
brand: "Confidential"
group_id: "CRCLEBESP"
id: "d703187ac59976b066c9b7ea01416bca"
msrp: "0"
name: "Clear Beyond Speaker Cable"
price: "5510"
product_type_unigram: "cable"
sku: "CRCLEBESP"
ss_msrp: (12) ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
ss_msrp_max: "0"
ss_msrp_min: "0"
ss_name_sizes: "Cardas Clear Beyond Speaker Cable"
ss_price: (12) ["6840", "5510", "9500", "8170", "8170", "12160", "10830", "14820", "13490", "17480", "16150", "18810"]
ss_price_max: "18810"
ss_price_min: "5510"
stock_id: "171038"
sub_sku: (12) ["1.5PSPSP", "1PSPSP", "2.5PSPSP", "2PBNBN", "2PSPSP", "3.5PSPSP", "3PSPSP", "4.5PSPSP", "4PSPSP", "5.5PSPSP", "5PSPSP", "6PSPSP"]
uid: "171038"
__proto__: Object
这些对象包含 3 个数组,每个数组我需要从名为“ss_msrp、ss_price 和 sub_sku”中提取值。每个对象中的每个数组都是相同的长度。例如,对象 0 有 3 个数组,每个数组有 12 个索引长。对象 1 有 3 个数组,每个数组有 6 个索引长。对象 3 有 3 个数组,每个数组有 8 个索引等。
我需要从每个数组中提取第 [n] 个项目并将其放入一个新对象中。每个对象将有 3 个键值对。例如:
ss_msrp = ["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]
ss_price = ["6840", "5510", "9500", "8170", "8170", "12160", "10830", "14820", "13490", "17480", "16150", "18810"]
sub_sku = ["1.5PSPSP", "1PSPSP", "2.5PSPSP", "2PBNBN", "2PSPSP", "3.5PSPSP", "3PSPSP", "4.5PSPSP", "4PSPSP", "5.5PSPSP", "5PSPSP", "6PSPSP"]
object1 = { msrp: "0", price: "6840", sku: "1.5PSPSP" }
object2 = { msrp: "0", price: "5510", sku: "1PSPSP" }
object3 = { msrp: "0", price: "9500", sku: "2.5PSPSP" }
object4 = { msrp: "0", price: "8170", sku: "2PBNBN" }
我的代码现在看起来像这样:
let objects = []
for (let i=0; i < $scope.results[0].sub_sku.length; i++){
objects.push({
sku: $scope.results[0].sub_sku[i],
msrp: $scope.results[0].ss_msrp[i],
price: $scope.results[0].ss_price[i]
})
}
这当然只会返回第一个对象的所需结果。我曾尝试将其嵌套在另一个 for 循环中,如下所示:
for (let x=0; x < $scope.results.length; x++){
for (let i=0; i < $scope.results[x].sub_sku.length; i++){
objects.push({
sku: $scope.results[x].sub_sku[i],
msrp: $scope.results[x].ss_msrp[i],
price: $scope.results[x].ss_price[i]
})
}
}
但我收到以下错误: 无法读取未定义的属性“长度”
有人有想法么?先感谢您。
解决方案
假设您的输入在数组中有 2 个对象
例子:
let examples=[
{
brand: "Confidential",
group_id: "CRCLEBESP",
id: "d703187ac59976b066csds9b7ea01416bca",
msrp: "0",
name: "Clear Beyond Speaker Cable",
price: "5510",
product_type_unigram: "cable",
sku: "CRCLEBESP",
ss_msrp:["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"],
ss_msrp_max: "0",
ss_msrp_min: "0",
ss_name_sizes: "Cardas Clear Beyond Speaker Cable",
ss_price:["6840", "5510", "9500", "8170", "8170", "12160", "10830", "14820", "13490", "17480", "16150", "18810"],
ss_price_max: "18810",
ss_price_min: "5510",
stock_id: "171038",
sub_sku:["1.5PSPSP", "1PSPSP", "2.5PSPSP", "2PBNBN", "2PSPSP", "3.5PSPSP", "3PSPSP", "4.5PSPSP", "4PSPSP", "5.5PSPSP", "5PSPSP", "6PSPSP"],
uid: "171038",
},
{
brand: "Confidential",
group_id: "CRCLEBESP",
id: "d703187ac59976b066c9b7ea01416bca",
msrp: "0",
name: "Clear Beyond Speaker Cable",
price: "5510",
product_type_unigram: "cable",
sku: "CRCLEBESP",
ss_msrp:["0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"],
ss_msrp_max: "0",
ss_msrp_min: "0",
ss_name_sizes: "Cardas Clear Beyond Speaker Cable",
ss_price:["6840", "5510", "9500", "8170", "8170", "12160", "10830", "14820", "13490", "17480", "16150", "18810"],
ss_price_max: "18810",
ss_price_min: "5510",
stock_id: "171038",
sub_sku:["1.5PSPSP", "1PSPSP", "2.5PSPSP", "2PBNBN", "2PSPSP", "3.5PSPSP", "3PSPSP", "4.5PSPSP", "4PSPSP", "5.5PSPSP", "5PSPSP", "6PSPSP"],
uid: "171038",
}]
您可以处理2个案例,
- 如果您的对象数组包含2 个相同的产品,并且您需要 2 个重复的对象,其中包含price、sku和msrp ,那么这是解决方案之一。
let result = examples.map(row=>{
return row.ss_msrp.map((e,i)=>{
return {
msrp:row.ss_msrp[i],
sku:row.sub_sku[i],
price:row.ss_price[i]
};
});
});
Output:
// console.log(result);
// (2) [Array(12), Array(12)]
// It will print an array which contains array of objects of each product
// eg: [[{msrp: "0", sku: "5PSPSP", price: "16150"},....(12 entries)],[{msrp: "0", sku: "5PSPSP", price: "16150"},....(12 entries)]]
2.如果您不希望重复的产品也引用产品 id,如果 id 是唯一的,那么您也可以这样做。
let result = {};
examples.map(row=>{
result[row.id]= row.ss_msrp.map((e,i)=>{
return {
msrp:row.ss_msrp[i],
sku:row.sub_sku[i],
price:row.ss_price[i]
};
});
});
Output:
//console.log(result);
// {d703187ac59976b066c9b7ea01416bca: (12) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]}
推荐阅读
- angular - 使用 mat-paginator angular 5 材质显示页码
- javascript - Vue.js,在 g 元素上附加 @click
- teechart - Angular 2+ 的图表控件(Steema 控件)
- google-cloud-platform - 如何在谷歌云平台获取备份数据库
- asp.net - Aspnetboilerplate AbpSession.GetTenantId() 与 AbpSession.TenantId
- visual-studio-code - VS Code 扩展如何在上下文中编辑?
- google-cloud-platform - 如何加快谷歌云语音
- javascript - 如何从 Map 对象中过滤某些条目?
- zos - MVS 上的 IBM Window Services (DWS) csrevw 函数
- c# - Xamarin 表单绑定。如何根据某些条件显示 3 个不同字符串中的 1 个