skip to Main Content

I’m trying to get the first element of each object ip and then concat them and push them in an array, then take the second element and so on..

here is the grouped by ip array

{
  "ip1.x.x.x": [
    [
      {
        "coin": "coin1"
      },
      {
        "coin": "coin2"
      },
      {
        "coin": "coin3"
      },
      {
        "coin": "coin4"
      },
      {
        "coin": "coin5"
      }
    ],
    [
      {
        "coin": "coin6"
      },
      {
        "coin": "coin7"
      },
      {
        "coin": "coin8"
      },
      {
        "coin": "coin9"
      }
    ]
  ],
  "ip2.x.x.x": [
    [
      {
        "coin": "coin10"
      },
      {
        "coin": "coin11"
      },
      {
        "coin": "coin12"
      },
      {
        "coin": "coin13"
      },
      {
        "coin": "coin14"
      }
    ],
    [
      {
        "coin": "coin15"
      },
      {
        "coin": "coin16"
      }
    ]
  ],
  "ip3.x.x.x": [
    [
      {
        "coin": "coin17"
      },
      {
        "coin": "coin18"
      },
      {
        "coin": "coin19"
      },
      {
        "coin": "coin20"
      },
      {
        "coin": "coin21"
      }
    ],
    [
      {
        "coin": "coin22"
      },
      {
        "coin": "coin23"
      },
      {
        "coin": "coin24"
      }
    ]
  ],
  etc..
}

the expected result should be as follows

[
  [
    {
      "coin": "coin1"
    },
    {
      "coin": "coin2"
    },
    {
      "coin": "coin3"
    },
    {
      "coin": "coin4"
    },
    {
      "coin": "coin5"
    },
    {
      "coin": "coin10"
    },
    {
      "coin": "coin11"
    },
    {
      "coin": "coin12"
    },
    {
      "coin": "coin13"
    },
    {
      "coin": "coin14"
    },
    {
      "coin": "coin17"
    },
    {
      "coin": "coin18"
    },
    {
      "coin": "coin19"
    },
    {
      "coin": "coin20"
    },
    {
      "coin": "coin21"
    }
  ],
  [
    {
      "coin": "coin6"
    },
    {
      "coin": "coin7"
    },
    {
      "coin": "coin8"
    },
    {
      "coin": "coin9"
    },
    {
      "coin": "coin15"
    },
    {
      "coin": "coin16"
    },
    {
      "coin": "coin22"
    },
    {
      "coin": "coin23"
    },
    {
      "coin": "coin24"
    }
  ]
]

as you can see I took the 1st element in the array of each ip and concat and pushed them to new array, then I took the 2nd element, and if there is 3rd element I’ll keep doing so till the object keys are done.

Now what I’ve tried to do is the following

const chunkArray = (array, chunkSize) => {
  return Array.from({
      length: Math.ceil(array.length / chunkSize)
    }, (_, index) =>
    array.slice(index * chunkSize, (index + 1) * chunkSize)
  );
};

let data_in_chunks_array = [];
let data_obj = {};

for (let i = 0; i < Object.keys(groupedByIp).length; i++) {
  const ip = Object.keys(groupedByIp)[i];

  let data_in_chunks_ip = chunkArray(groupedByIp[ip], 5);

  data_obj[ip] = data_in_chunks_ip;
}

console.log('data_obj', data_obj);
<script>
  let groupedByIp = {
    'ip1.x.x.x': [{
        coin: 'coin1',
      },
      {
        coin: 'coin2',
      },
      {
        coin: 'coin3',
      },
      {
        coin: 'coin4',
      },
      {
        coin: 'coin5',
      },
      {
        coin: 'coin6',
      },
      {
        coin: 'coin7',
      },
      {
        coin: 'coin8',
      },
      {
        coin: 'coin9',
      },
    ],
    'ip2.x.x.x': [{
        coin: 'coin10',
      },
      {
        coin: 'coin11',
      },
      {
        coin: 'coin12',
      },
      {
        coin: 'coin13',
      },
      {
        coin: 'coin14',
      },
      {
        coin: 'coin15',
      },
      {
        coin: 'coin16',
      },
    ],
    'ip3.x.x.x': [{
        coin: 'coin17',
      },
      {
        coin: 'coin18',
      },
      {
        coin: 'coin19',
      },
      {
        coin: 'coin20',
      },
      {
        coin: 'coin21',
      },
      {
        coin: 'coin22',
      },
      {
        coin: 'coin23',
      },
      {
        coin: 'coin24',
      },
    ],
  };
</script>

This will return the first example data. Now I can’t figure out the second part where I take the 1st element of each ip array and concat them and keep doing so till they are all have been taken, like (remove at index) for example.

2

Answers


  1. Chosen as BEST ANSWER

    I com up with a solution as follows

    const get_data_in_chunks_new = (dataGroupedByIp) => {
      return new Promise((resolve) => {
        let data_obj = {};
        let data_obj_tmp = {};
        let data_in_chunks_new = [];
    
        const chunkArray = (array, chunkSize) => {
          return Array.from({ length: Math.ceil(array.length / chunkSize) }, (_, index) =>
            array.slice(index * chunkSize, (index + 1) * chunkSize)
          );
        };
    
        const customizer = (objValue, srcValue) => {
          if (_.isArray(objValue)) {
            return objValue.concat(srcValue);
          }
        };
    
        for (let i = 0; i < Object.keys(dataGroupedByIp).length; i++) {
          const ip = Object.keys(dataGroupedByIp)[i];
    
          let data_in_chunks_ip = chunkArray(dataGroupedByIp[ip], 5);
    
          data_obj[ip] = {};
    
          for (let y = 0; y < data_in_chunks_ip.length; y++) {
            const chunk = data_in_chunks_ip[y];
    
            data_obj[ip][y] = chunk;
          }
        }
    
        let args = [];
    
        for (let i = 0; i < Object.keys(data_obj).length; i++) {
          const ip = Object.keys(data_obj)[i];
    
          args.push(data_obj[ip]);
        }
    
        data_obj_tmp = _.mergeWith(...args, customizer);
    
        for (let i = 0; i < Object.keys(data_obj_tmp).length; i++) {
          const key = Object.keys(data_obj_tmp)[i];
    
          data_in_chunks_new.push(data_obj_tmp[key]);
        }
    
        return resolve(data_in_chunks_new);
      });
    };
    
    get_data_in_chunks_new({
      "ip1.x.x.x": [
        {
          coin: "coin1",
        },
        {
          coin: "coin2",
        },
        {
          coin: "coin3",
        },
        {
          coin: "coin4",
        },
        {
          coin: "coin5",
        },
        {
          coin: "coin6",
        },
        {
          coin: "coin7",
        },
        {
          coin: "coin8",
        },
        {
          coin: "coin9",
        },
      ],
      "ip2.x.x.x": [
        {
          coin: "coin10",
        },
        {
          coin: "coin11",
        },
        {
          coin: "coin12",
        },
        {
          coin: "coin13",
        },
        {
          coin: "coin14",
        },
        {
          coin: "coin15",
        },
        {
          coin: "coin16",
        },
      ],
      "ip3.x.x.x": [
        {
          coin: "coin17",
        },
        {
          coin: "coin18",
        },
        {
          coin: "coin19",
        },
        {
          coin: "coin20",
        },
        {
          coin: "coin21",
        },
        {
          coin: "coin22",
        },
        {
          coin: "coin23",
        },
        {
          coin: "coin24",
        },
      ],
    }).then(result => console.log(result));
    

    It seems to work for me, it's based on lodash lib


  2. This conversion has the concept of [transposing a maxtrix), together with concatenating the inner arrays (for which flatMap can serve), and ignoring the keys of the original object (for which we can use Object.values).

    So:

    const transposeFlat = arr => arr[0].map((_, i) => arr.flatMap(row => row[i]));
    
    // Your example data:
    const data = {"ip1.x.x.x": [[{"coin": "coin1"},{"coin": "coin2"},{"coin": "coin3"},{"coin": "coin4"},{"coin": "coin5"}],[{"coin": "coin6"},{"coin": "coin7"},{"coin": "coin8"},{"coin": "coin9"}]],"ip2.x.x.x": [[{"coin": "coin10"},{"coin": "coin11"},{"coin": "coin12"},{"coin": "coin13"},{"coin": "coin14"}],[{"coin": "coin15"},{"coin": "coin16"}]],"ip3.x.x.x": [[{"coin": "coin17"},{"coin": "coin18"},{"coin": "coin19"},{"coin": "coin20"},{"coin": "coin21"}],[{"coin": "coin22"},{"coin": "coin23"},{"coin": "coin24"}]],}
    const res = transposeFlat(Object.values(data));
    console.log(res);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search