我的目标是,如果digital_assets和产品中的id匹配,然后从products对象中获取digital_assets和ProductName的URL值。我能够遍历该对象并获取digital_assets和产品的值,但需要一些帮助来基于ID比较这两个对象以获得URL和ProductName的值。下面是我到目前为止所做的。
var data = [{
"digital_assets": [{
"id": "AA001",
"url": "https://via.placeholder.com/150"
},{
"id": "AA002",
"url": "https://via.placeholder.com/150"
}]
}, {
"products": [{
"id": ["BB001", "AA001"],
"ProductName": "PROD 485"
},{
"id": ["BB002", "AA002"],
"ProductName": "PROD 555"
}]
}
];
$.each(data, function () {
var data = this;
//console.log(data);
$.each(data.digital_assets, function () {
var dAssets = this,
id = dAssets['id'];
// console.log(id);
});
$.each(data.products, function () {
var proData = this,
prod_id = proData['id'];
// console.log(prod_id);
$.each(prod_id, function () {
var arr_id = this;
console.log(arr_id);
});
});
});
我需要创建新数组并将值推入新数组吗?然后将这些数组concat()合并为1。 ?失去任何帮助将不胜感激。
答案 0 :(得分:1)
这是您可以通过Array.reduce
,Array.includes
,Object.entries
和Array.forEach
进行此操作的一种方式:
var data = [{ "digital_assets": [{ "id": "AA001", "url": "https://via.placeholder.com/150" }, { "id": "AA002", "url": "https://via.placeholder.com/150" } ] }, { "products": [{ "id": ["BB001", "AA001"], "ProductName": "PROD 485" }, { "id": ["BB002", "AA002"], "ProductName": "PROD 555" } ] } ]
const result = data.reduce((r,c) => {
Object.entries(c).forEach(([k,v]) =>
k == 'digital_assets'
? v.forEach(({id, url}) => r[id] = ({ id, url }))
: v.forEach(x => Object.keys(r).forEach(k => x.id.includes(k)
? r[k].ProductName = x.ProductName
: null))
)
return r
}, {})
console.log(Object.values(result))
答案 1 :(得分:1)
您可以使用Array.prototype.find
,Array.prototype.includes
和Array.prototype.map
非常优雅地实现这一目标。
let data = [
{
"digital_assets": [
{
"id": "AA001",
"url": "https://via.placeholder.com/150"
},
{
"id": "AA002",
"url": "https://via.placeholder.com/150"
}
]
},
{
"products": [
{
"id": ["BB001", "AA001"],
"ProductName": "PROD 485"
},
{
"id": ["BB002","AA002"],
"ProductName": "PROD 555"
}
]
}
];
// Find the 'digital_assets' array
let assets = data.find(d => d['digital_assets'])['digital_assets'];
// Find the 'products' array
let products = data.find(d => d['products'])['products'];
// Return an array of composed asset objects
let details = assets.map(a => {
return {
id : a.id,
url : a.url
name : products.find(p => p.id.includes(a.id)).ProductName
};
});
console.log(details);
答案 2 :(得分:0)
已更改答案以适合您的需求:
var data = [
{
"digital_assets": [
{
"id": "AA001",
"url": "https://via.placeholder.com/150"
},
{
"id": "AA002",
"url": "https://via.placeholder.com/150"
}
]
},
{
"products": [
{
"id": ["BB001", "AA001"],
"ProductName": "PROD 485"
},
{
"id": ["BB002","AA002"],
"ProductName": "PROD 555"
}
]
}
]
let matchingIds = [];
let data_assetsObject = data.find(element => {
return Object.keys(element).includes("digital_assets")
})
let productsObject = data.find(element => {
return Object.keys(element).includes("products")
})
data_assetsObject["digital_assets"].forEach(da => {
productsObject["products"].forEach(product => {
if (product.id.includes(da.id)){
matchingIds.push({
url: da.url,
productName: product.ProductName
})
}
})
})
console.log(matchingIds);
有效的提琴:https://jsfiddle.net/z2ak1fvs/3/
希望能有所帮助。如果您不想使用新数组,也可以将各自的数据存储在要遍历的元素中。
编辑: 我想我知道我为什么被拒绝。我的示例通过使数据成为对象而不是数组来工作。更改了代码段以更清楚地显示这一点。 为什么数据仍然是数组?有什么原因吗?还是可以将其转换为对象?
编辑nr2: 我根据您的评论理解了代码,从而更改了代码以满足期望。它现在使用您的数据结构,无论数据中有什么内容,您现在都可以搜索包含digital_assets / products属性的对象。
欢呼
答案 3 :(得分:-1)
https://jsfiddle.net/2b1zutvx/
使用地图。
var myobj = data[0].digital_assets.map(function(x) {
return {
id: x.id,
url: x.url,
ProductName: data[1].products.filter(f => f.id.indexOf(x.id) > -1).map(m => m.ProductName)
};
});