我想将不同数组中的对象的值连接到一边。
我试图将json中接收的数据值输出到console.log。
我想把成分列表中的值放到列表数组中。
console.log(detail);
{
List: [
{
id: 120,
content: "stack-overflow",
functionalList: [
{
id: 832,
},
],
},
{
id: 230,
content: "heap-overflow",
functionalList: [
{
id: 24,
},
],
},
],
ListValue: [
{
IngredientList: [
{
id: 1,
value: 43
},
{
id: 23,
value: 23
},
],
},
],
},
]);我希望将ListValue -> IngredientList值放入List数组对象中。
我怎么能这样做呢?我试了一天,但这对我来说很难。
{
List: [
{
id: 120,
content: "stack-overflow",
value: 43
functionalList: [
{
id: 832,
functionalId: 37
},
],
},
{
id: 230,
content: "heap-overflow",
value: 23
functionalList: [
{
id: 24,
functionalId: 12
},
],
},
],
ListValue: [
{
IngredientList: [
{
id: 1,
value: 43
},
{
id: 23,
value: 23
},
],
},
],
},
]);发布于 2022-04-14 16:41:23
即使在ListValue中有多个对象,这也应该可以以可变的方式工作:
data.List = [
...data.List,
...data.ListValue.reduce((arr, el) => {
arr.push(...el.IngredientList);
return arr;
}, []),
];发布于 2022-04-14 16:46:24
还不清楚IngredientList的哪个值应该放在List的哪个项中。假设您总是希望第一个值与第一个项对,第二个值与第二个项成对,依此类推……
const obj = {
List: [
{
id: 120,
content: "stack-overflow",
functionalList: [
{
id: 832,
},
],
},
{
id: 230,
content: "heap-overflow",
functionalList: [
{
id: 24,
},
],
},
],
ListValue: [
{
IngredientList: [
{
id: 1,
value: 43,
},
{
id: 23,
value: 23,
},
],
},
],
};
const ingridientsValue = obj.ListValue[0].IngredientList.map(el => el.value); // [43, 23]
for (const item of obj.List) item.value = ingridientsValue.shift();
console.log(obj.List);
发布于 2022-04-14 17:07:03
,我已经解决了这个问题。请看这里: https://jsfiddle.net/bowtiekreative/o5rhy7c1/1/
首先,需要验证您的JSON。删除")“和”额外",“
指令
H 114将arr数组记录到控制台。H 215g 216>
示例:
var json = {
"List":[
{
"id":120,
"content":"stack-overflow",
"functionalList":[
{
"id":832
}
]
},
{
"id":230,
"content":"heap-overflow",
"functionalList":[
{
"id":24
}
]
}
],
"ListValue":[
{
"IngredientList":[
{
"id":1,
"value":43
},
{
"id":23,
"value":23
}
]
}
]
};
var arr = [];
for (var i = 0; i < json.ListValue.length; i++) {
for (var j = 0; j < json.ListValue[i].IngredientList.length; j++) {
arr.push(json.ListValue[i].IngredientList[j].value);
}
}
console.log(arr)https://stackoverflow.com/questions/71874841
复制相似问题