Quiero concatenar los valores de los objetos en diferentes matrices a un lado.
Traté de enviar el valor de los datos recibidos en json a console.log.
Quiero poner los valores de la Lista de ingredientes en la matriz de la Lista.
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 }, ], }, ], }, ]); Quiero poner los valores de ListValue -> IngredientList en el objeto de matriz de lista.
¿Cómo puedo hacerlo de esta manera? Lo he estado intentando todo el día, pero es difícil para mí.
{ 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 }, ], }, ], }, ]);He resuelto esto. Míralo aquí: https://jsfiddle.net/bowtiekreative/o5rhy7c1/1/
Primero, su JSON necesita ser validado. Retire el ")" y el "," extra
Instrucciones
Ejemplo:
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)Esto debería funcionar en un enfoque mutable, incluso si tiene varios objetos dentro de ListValue:
data.List = [ ...data.List, ...data.ListValue.reduce((arr, el) => { arr.push(...el.IngredientList); return arr; }, []), ];No está claro qué valor de IngredientList debe ir en qué elemento de List . Supongamos que siempre desea emparejar el primer valor con el primer elemento, el segundo con el segundo, y así sucesivamente...
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);