我正在嘗試forEach json中的值,并通過使用里面的map函式將所需的值推送到陣列中。
[
{
"detail": {
"id": 89,
"content": "123",
"type": "one",
"company": "minsu",
"certificationNum": "minsu"
},
"ingredientList": [
{
"id": 161,
"content": "32523523",
"functionalList": [
{
"id": 129,
"valueUnit": "mg"
}
]
},
{
"id": 162,
"content": "162",
"functionalList": [
{
"id": 130,
"valueUnit": "cm"
}
]
}
]
},
{
"detail": {
"id": 91,
"content": "332",
"type": "two",
"company": "sss",
"certificationNum": "1123-522"
},
"ingredientList": [
{
"id": 164,
"content": "hi",
"functionalList": [
{
"id": 132,
"valueUnit": "kg"
},
{
"id": 133,
"valueUnit": "g"
}
]
},
{
"id": 165,
"content": "succes",
"functionalList": [
{
"id": 134,
"valueUnit": "fie"
},
{
"id": 135,
"valueUnit": "fy"
}
]
}
]
}
]
就是一個簡單的json,
我想把json資料值push到一個陣列里,把它們作為這些值。
[
{
"content": "123",
"type": "one",
"certificationNum": "minsu",
"functionalList": {
{
"id": 129,
"valueUnit": "mg"
},
{
"id": 130,
"valueUnit": "cm"
}
}
},
{
"content": "332",
"type": "two",
"certificationNum": "1123-522",
"functionalList": {
{
"id": 132,
"valueUnit": "kg"
},
{
"id": 133,
"valueUnit": "g"
},
{
"id": 134,
"valueUnit": "fie"
},
{
"id": 135,
"valueUnit": "fy"
}
}
}
]
let SeedDetailArray = new Array();
SeedValue.forEach(function(id: any) {
Object.keys(id).forEach(function(props) {
SeedDetailArray.push({
content: id[props]["content"],
type: id[props]["type"],
certificationNum: id[props]["certificationNum"],
functionalList: id[props]["functionalList"],
});
});
})
控制臺.log(種子值);
[
{
"content": "123",
"type": "one",
"certificationNum": "minsu",
},
{
"content": "332",
"type": "two",
"certificationNum": "1123-522",
}
]
我放入Seed Value json了Seed Detail ArrayforEachdetail陳述句,但我不知道如何將值放入functionList.
我如何創建這樣的json?
[
{
"content": "123",
"type": "one",
"certificationNum": "minsu",
"functionalList": {
{
"id": 129,
"valueUnit": "mg"
},
{
"id": 130,
"valueUnit": "cm"
}
}
},
{
"content": "332",
"type": "two",
"certificationNum": "1123-522",
"functionalList": {
{
"id": 132,
"valueUnit": "kg"
},
{
"id": 133,
"valueUnit": "g"
},
{
"id": 134,
"valueUnit": "fie"
},
{
"id": 135,
"valueUnit": "fy"
}
}
}
]
uj5u.com熱心網友回復:
您可以使用 tmp 陣列來保存functionalList
let SeedDetailArray = new Array();
SeedValue.forEach(function(id) {
let tmp = []
id.ingredientList.forEach(v1=>{
v1.functionalList.forEach(v2=>{
tmp.push(v2)
})
})
Object.keys(id).forEach(function(props) {
SeedDetailArray.push({
content: id[props]["content"],
type: id[props]["type"],
certificationNum: id[props]["certificationNum"],
functionalList: tmp,
});
});
})
uj5u.com熱心網友回復:
SeedValue.forEach(function(id: any) {
Object.keys(id).forEach(function(props) {
// YOUR CODE
});
})
您正在遍歷鍵,這將為每個物件回傳兩個鍵,detail并且ingredientList. 因此,您的代碼將生成一個包含undefined內容的物件。
我使用了帶有物件解構的映射并使用臨時陣列來獲取功能串列。
let a = [
{
detail: {
id: 89,
content: "123",
type: "one",
company: "minsu",
certificationNum: "minsu",
},
ingredientList: [
{
id: 161,
content: "32523523",
functionalList: [
{
id: 129,
valueUnit: "mg",
},
],
},
{
id: 162,
content: "162",
functionalList: [
{
id: 130,
valueUnit: "cm",
},
],
},
],
},
{
detail: {
id: 91,
content: "332",
type: "two",
company: "sss",
certificationNum: "1123-522",
},
ingredientList: [
{
id: 164,
content: "hi",
functionalList: [
{
id: 132,
valueUnit: "kg",
},
{
id: 133,
valueUnit: "g",
},
],
},
{
id: 165,
content: "succes",
functionalList: [
{
id: 134,
valueUnit: "fie",
},
{
id: 135,
valueUnit: "fy",
},
],
},
],
},
];
console.log(a.map(({ detail: { content, type, certificationNum }, ingredientList }) => {
let functionalList = ingredientList.map(({ functionalList }) => functionalList);
return {
content: content,
type: type,
certificationNum: certificationNum,
functionalList,
};
}));
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/456226.html
