Home > Blockchain >  I want to combine the values ​of objects in different arrays by sorting them one side
I want to combine the values ​of objects in different arrays by sorting them one side

Time:04-15

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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)
  • Related