Get all the nested arrays from main array and merge each of them according to keys Knockout.Js

Muhammad Mudassar

I am trying to merge all the nested array with same keys, there are more then 100 nested arrays, I want them all to be merged when key is same.

I have tried to merge arrays with a little harder way (which I don't want because I have 100 plus nested arrays).

var keys = [];
var stream = {arr1: [], arr2:[], arr3:[]}; // Nested Arrays
for(var i =0;i<arr_main.length;i++){
  for(var key in arr_main[i]){
   if(keys.find(x => x === key))
   {
     if(key == "arr1")
     {
     let arr = stream.arr1
     let union = [...new Set([...arr, ...arr_main[i][key]])];
     stream.arr1 = union
     }
     else if(key == "arr2")
     {
     let arr = stream.arr2
     let union = [...new Set([...arr, ...arr_main[i][key]])];
     stream.arr2 = union
     }
     else if(key == "arr3")
     {
     let arr = stream.arr3
     let union = [...new Set([...arr, ...arr_main[i][key]])];
     stream.arr3 = union
     }
   }else{
     if(key == "arr1")
     stream.arr1 = arr_main[i][key]
     else if(key == "arr2")
     stream.arr2 = arr_main[i][key]
     else if(key == "arr3")
     stream.arr3 = arr_main[i][key]
   }
   if(!keys.find(x => x === key))
   keys.push(key);
  }
 }

Expected Output:

{"arr1":[{"val1":"val1","val2":"val2"},{"val1":"val111","val2":"val222"}],"arr2":[{"val1":"val11","val2":"val22"},{"val1":"val1","val2":"val2"}],"arr3":[{"val1":"val111","val2":"val222"},{"val1":"val11","val2":"val22"}]}

Link to jsFiddle https://jsfiddle.net/kbjcsar6/13/

adiga

You could reduce the array. Inside, loop through the entries of each object. Add each key to the accumulator and merge them.

const input=[{arr1:[{val1:"val1",val2:"val2"}],arr2:[{val1:"val11",val2:"val22"}],arr3:[{val1:"val111",val2:"val222"}]},{arr1:[{val1:"val111",val2:"val222"}],arr2:[{val1:"val1",val2:"val2"}],arr3:[{val1:"val11",val2:"val22"}]}];

const output = input.reduce((acc, o) => {
  Object.entries(o).forEach(([key, arr]) => {
    acc[key] = acc[key] || [];
    acc[key].push(...arr)
  })
  return acc
}, {})

console.log(output)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to get all keys with values from nested objects

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

Get union of keys of all objects in js array using reduce

分類Dev

C#: how to get all values for keys from JS Object?

分類Dev

Sum of nested arrays according to their indexes

分類Dev

How to create a data frame from each url and then merge them?

分類Dev

Get count from Array of arrays

分類Dev

merge array with array of arrays

分類Dev

How to get all documents from a firestore collection and return them in an array list?

分類Dev

merge array with unique keys

分類Dev

get all keys from spring ReloadableResourceBundleMessageSource

分類Dev

How to get the keys as string array from map?

分類Dev

vue.js - recursive components doesn't get updated when using nested array from raw json

分類Dev

Import two modules in Main file, and call methods in them from each other

分類Dev

get value from nested json array

分類Dev

How to generate an object with nested keys based on an array of arrays that hold it's key locations?

分類Dev

Update an array item in knockout js

分類Dev

merge 2 seperate arrays matching keys

分類Dev

Merge array according to match column values in PHP

分類Dev

What's the best way to merge to arrays based on key values in each array?

分類Dev

Get all the keys for a class

分類Dev

Array flip and merge multiple arrays

分類Dev

Load all csv/txt files from one directory and merge them via python

分類Dev

How to get all the keys values from Redis Cache in C#?

分類Dev

How to get distinct values from an array in mongoDB and group them by ID

分類Dev

Merge two multi-dimensional arrays by matching on a chosen column and returns a new array with all rows and columns

分類Dev

Get all rows and colums from array

分類Dev

Delete main array when nested array is empty

分類Dev

Immutable JS - merge creates a list of map objects from array

Related 関連記事

  1. 1

    How to get all keys with values from nested objects

  2. 2

    How to get the combination of array values from nested arrays in an array of objects

  3. 3

    Get union of keys of all objects in js array using reduce

  4. 4

    C#: how to get all values for keys from JS Object?

  5. 5

    Sum of nested arrays according to their indexes

  6. 6

    How to create a data frame from each url and then merge them?

  7. 7

    Get count from Array of arrays

  8. 8

    merge array with array of arrays

  9. 9

    How to get all documents from a firestore collection and return them in an array list?

  10. 10

    merge array with unique keys

  11. 11

    get all keys from spring ReloadableResourceBundleMessageSource

  12. 12

    How to get the keys as string array from map?

  13. 13

    vue.js - recursive components doesn't get updated when using nested array from raw json

  14. 14

    Import two modules in Main file, and call methods in them from each other

  15. 15

    get value from nested json array

  16. 16

    How to generate an object with nested keys based on an array of arrays that hold it's key locations?

  17. 17

    Update an array item in knockout js

  18. 18

    merge 2 seperate arrays matching keys

  19. 19

    Merge array according to match column values in PHP

  20. 20

    What's the best way to merge to arrays based on key values in each array?

  21. 21

    Get all the keys for a class

  22. 22

    Array flip and merge multiple arrays

  23. 23

    Load all csv/txt files from one directory and merge them via python

  24. 24

    How to get all the keys values from Redis Cache in C#?

  25. 25

    How to get distinct values from an array in mongoDB and group them by ID

  26. 26

    Merge two multi-dimensional arrays by matching on a chosen column and returns a new array with all rows and columns

  27. 27

    Get all rows and colums from array

  28. 28

    Delete main array when nested array is empty

  29. 29

    Immutable JS - merge creates a list of map objects from array

ホットタグ

アーカイブ