我想把价值观结合起来​对不同数组中的对象进行排序



我想连接这些值​​将不同阵列中的对象放置到一侧
我试图将json中接收到的数据值输出到console.log。

我想把价值观​​在Ingredient List中的List数组中。

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
},
],
},
],
},
]);

即使ListValue:中有多个对象,这也应该以可变的方法工作

data.List = [
...data.List,
...data.ListValue.reduce((arr, el) => {
arr.push(...el.IngredientList);
return arr;
}, []),
];

不清楚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);

我已经解决了这个问题。请在此处查看:https://jsfiddle.net/bowtiekreative/o5rhy7c1/1/

首先,您的JSON需要经过验证。删除"("以及额外的">

说明

  1. 创建一个名为arr的空数组
  2. 在ListValue数组中循环
  3. 对于ListValue数组中的每个项,循环遍历IngredientList数组
  4. 对于IngredientList数组中的每个项,将value属性推送到arr数组中
  5. 将arr数组记录到控制台

示例:

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)

最新更新