Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
I want to combine the values ​of objects in different arrays by sorting them one side

I want to concatenate the values ​​of objects in different arrays to one side.
I tried to output the data value received in json to console.log.

I want to put the values ​​in the Ingredient List into the List array.

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

I want to put ListValue -> IngredientList value values ​​into List array object.
How can I do it this way? I've been trying all day, but it's hard for me.

{
     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
          },
        ],
      },
    ],
  },
]);
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I have solved this. Check it out here: https://jsfiddle.net/bowtiekreative/o5rhy7c1/1/

First your JSON needs to be validated. Remove the ")" and the extra ","

Instructions

  1. Create an empty array called arr.
  2. Loop through the ListValue array.
  3. For each item in the ListValue array, loop through the IngredientList array.
  4. For each item in the IngredientList array, push the value property into the arr array.
  5. Log the arr array to the console.

Example:

    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)
about 4 years ago · Juan Pablo Isaza Denunciar

0

This should work in a mutable approach, even if you have multiple objects inside ListValue:

data.List = [
  ...data.List,
  ...data.ListValue.reduce((arr, el) => {
    arr.push(...el.IngredientList);
    return arr;
  }, []),
];
about 4 years ago · Juan Pablo Isaza Denunciar

0

It's not clear which value of the IngredientList should go in which item of List. Supposing you always want to pair the first value with the first item, the second with the second, and so on...

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);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda