Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

95
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda