下面是我从API调用中获得的数据
{
"articles": {
"articles": [
{
"active": true,
"scanCodes": [
"356448",
"875736475896"
],
"stock": {
"1": {
"stockCurrent": 6.0,
"stockCustomValue": 0.0,
"stockMinimum": 4.0,
"stockTarget": 14.0
}
},
"taxRate": 7.0
},
{
"active": true,
"scanCodes": [
"682360596",
"2355235",
"235235235"
],
"stock": {
"1": {
"stockCurrent": 1.0,
"stockCustomValue": 1.0,
"stockMinimum": 2.0,
"stockTarget": 6.0
},
"3": {
"stockCurrent": 4.0,
"stockCustomValue": 0.0,
"stockMinimum": 2.0,
"stockTarget": 7.0
}
},
"taxRate": 7.0
},
{
"active": true,
"scanCodes": [
"50001112547160"
],
"stock": {
"1": {
"stockCurrent": 1.0,
"stockCustomValue": 1.0,
"stockMinimum": 2.0,
"stockTarget": 7.0
},
"2": {
"stockCurrent": 2.0,
"stockCustomValue": 0.0,
"stockMinimum": 0.0,
"stockTarget": 1.0
}
},
"taxRate": 7.0
},
]
}
}我只需要每一篇文章的唯一对象键,比如"1","2","3","5"....etc
我已经尝试过这个代码,还有很多其他的,但是还没有得到正确的结果,请帮帮我,我会非常感谢你的。
resp.data.articles.articles.
reduce((p, c) => {
let { stock } = c
return { ...p.stock + Object.keys(stock) };
}, {})发布于 2022-08-16 12:13:36
以下是一个简单的解决方案:
const arr = [];
data.articles.articles.forEach(article => arr.push(...Object.keys(article.stock)));
const requiredList = [...new Set(arr)];发布于 2022-08-16 11:56:01
这里要循环的是作为数组的项目,每个项目都有一个对象的库存数组,您需要键。
let art = data.articles.articles;
let stockIds = [];
// loop through the articles
for (let a in art) {
// loop throgh the stock array in each article
for (let s in art[a].stock) {
// Grab the stock key
stockIds.push(s);
}
}
//remove duplicates
let uniqueStockIds = uniq = [...new Set(stockIds)]https://stackoverflow.com/questions/73373428
复制相似问题