{
"product_hot": {
"default": {
"name": "Rose",
"values": [
"product_rose",
"product_default"
]
},
"exclusive": {
"name": "Cherry Blossom",
"values": [
"product_cherryBlossom",
"product_exclusive",
]
}
},
}
我已经从数据库中检索了结果。它是一个数组:
console.log(this.productType) // result: ['product_cherryBlossom', 'product_cherryBlossom'];
如何将此结果与上面的对象进行比较,以获得 key :exclusive
。
我的问题可能有很多错误。请给我更多反馈。谢谢
最佳答案
假设下面是可比较的对象
let compObj = {
"product_hot": {
"default": {
"name": "Rose",
"values": [
"product_rose",
"product_default"
]
},
"exclusive": {
"name": "Cherry Blossom",
"values": [
"product_cherryBlossom",
"product_exclusive",
]
}
},
}
那么你可以尝试类似的事情
// Function that returns keys as an array
getCategoryKey(resp, compObj) { // pass the response and compObj as arguments to this function
let category = [];
resp.forEach(item => {
Object.keys(compObj.product_hot).forEach(categ => {
if(compObj.product_hot[categ].values.includes(item)) {
category.push(categ);
}
}
}
);
console.log('Categories corresponding to your response', category);
return category;
}
关于javascript - 在vuejs中比较数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69573805/