我有一个像这样的json数组:
digitalDataProductsArray = [{
'id': '',
'name': '',
'type': '',
'subType': '',
'accountType': '',
'price': {
'fullRetailPrice':'',
'monthlyRecurringCharge':'',
'dueToday':'',
'dueMonthly':''
},
'attributes': {
'lineNumber':'',
'SOC':'',
'SKU':'',
'manufacturer':'',
'color':'',
'condition':'',
'usingEIP':'',
'EIPmonths':''
}
}];
我有一个像这样的json对象(不是完整的对象):
{
"productdetails" :
[
{
"DeviceId" : "456735",
"Family" : "mobile phone",
},
"Features" :
[
{
"Header" : "4.7 Retina Display",
},
{
"Header" : "5.5 Retina Display",
}
],
{
"DeviceId" : "456736",
"Family" : "Tablet",
}
]
}
product_Stails中有大约12个对象。
示例:
0:
Object
1
:
Object
2
:
Object
3
:
Object
4
:
Object
5
:
Object
6
:
Object
7
:
Object
8
:
Object
9
:
Object
10
:
Object
11
:
Object
并且每个对象都包含此值:
{
"DeviceId" : "456735",
"Family" : "mobile phone",
}
我需要弄清楚以下内容:
json对象的值将基于插入到json数组中 密钥映射要求如下:
id
应映射到DeviceId
name
应映射到Family
所以如果json对象中有2个对象,我需要创建
json数组中具有键的对象数量相等(例如id
,name
等)和空值
我尝试了以下但无法找到解决方案。
app.service('populateProductService', function ($window) {
var digitalDataProductsArray = $window.digitalData.events.products;
digitalDataProductsArray = [{
'id': '',
'name': '',
'type': '',
'subType': '',
'accountType': '',
'price': {
'fullRetailPrice':'',
'monthlyRecurringCharge':'',
'dueToday':'',
'dueMonthly':''
},
'attributes': {
'lineNumber':'',
'SOC':'',
'SKU':'',
'manufacturer':'',
'color':'',
'condition':'',
'usingEIP':'',
'EIPmonths':''
}
}];
console.log("digital data->events->products array: ", digitalDataProductsArray);
console.log(digitalDataProductsArray[0].id);
this.populateProductDetails = function (productDetails) {
var apiProductsArray = productDetails.data.productdetails;
console.log("API productdetails Array: ", apiProductsArray);
// digitalDataProductsArray[0].id = apiProductsArray[0].DeviceId;
for (var i = 0; i < apiProductsArray.length; i++) {
digitalDataProductsArray.push(digitalDataProductsArray); //creates 1 object with 13 arrays
// console.log(i); //prints 11
// digitalDataProductsArray[i].id = apiProductsArray[i].DeviceId;
// digitalDataProductsArray[i].type = apiProductsArray[i].type;
}
console.log("printing digital-data product array: ", digitalDataProductsArray);
};
});
这创建了一个包含13个数组的对象,每个数组又有一个包含13个数组的对象,依此类推,这是错误的。无法弄清楚这一点。