JavaScript How to split array of objects into chunks split array objects,split array objects javascript,split objects array

Im trying to split my array of 9 elements into array of 3 arrays, each with 3 elements.

const data = [{
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
];

const chunks = Array(3).fill(Array());
data.forEach((value, index) => chunks[index % chunks.length].push(value));
console.log(chunks);
Answer:1

This happens because fill is returning the same array to each element, hence pushing to either of the three is, in fact, pushing to the same reference.

Mentioned in the MDN docs:

The fill() method fills (modifies) all the elements of an array from a start index (default zero) to an end index (default array length) with a static value. It returns the modified array.

^-- a STATIC value. Read more about fill here

To make it clearer, this:

const chunks = Array(3).fill(Array());

can be "interpreted" as this:

const chunks = new Array(3);
var arr = new Array();
for (var i = 0; i < chunks.length; i++) chunks.push(arr);

So, basically, the same reference of the array is pushed to the original array; hence, altering arr means altering all the references of it, so all the references in chunks are pointing to the same array, so all are "altered", meaning they all will point to the same array and share the same values.

You can solve that in many ways, the fastest I can think of is using Array.from as done below, where the callback returns a new array everytime.

const data = [
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
  {
    name: 'a',
    surname: 'b'
  },
];

const chunks = Array.from({length: 3}, (_) => []);
data.forEach((value, index) => chunks[index%chunks.length].push(value));
console.log(chunks);
Answer:2

You could slice the array by the index and the wanted size.

var data = [{ name: 'a', surname: 'b' }, { name: 'a', surname: 'b' }, { name: 'a', surname: 'b' }, { name: 'a', surname: 'b' }, { name: 'a', surname: 'b' }, { name: 'a', surname: 'b' }, { name: 'a', surname: 'b' }, { name: 'a', surname: 'b' }, { name: 'a', surname: 'b' }],
    chunks = [],
    size = 3,
    i = 0;
    
while (i < data.length) chunks.push(data.slice(i, i += size));

console.log(chunks);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer:3
const chunks = Array(3).fill(Array());

You have filled the chunks array with 3 times the same array object. Spelled out:

const chunk = Array();
const chunks = Array(3).fill(chunk);

Which basically ends up being called in forEach:

data.forEach((value, index) => chunk.push(value));

Change it to:

const chunks = Array(3).fill(1).map(() => []);
Answer:4

I have string with below format const number = "21708.333333333336" Then I pass it to the function to get the number in this format "21,708.333333333336" Function I am using for to do this (I ...

I have string with below format const number = "21708.333333333336" Then I pass it to the function to get the number in this format "21,708.333333333336" Function I am using for to do this (I ...

I need to turn a string formatted like that: string = "John:31,Miranda:28" Onto this; obj = { "John" => 31, "Miranda" => 28 } I did this : const class = new Map(); array = string.split(","); ...

I need to turn a string formatted like that: string = "John:31,Miranda:28" Onto this; obj = { "John" => 31, "Miranda" => 28 } I did this : const class = new Map(); array = string.split(","); ...

  1. turn string into array
  2. turn string into int
  3. turn string into list python
  4. turn string into number
  5. turn string into int python
  6. turn string into datetime python
  7. turn string into json
  8. turn string into dictionary python
  9. turn string into array python
  10. turn string into array java
  11. turn string into bytes python
  12. turn string into json python
  13. turn string into int c++
  14. turn string into float python
  15. turn string into list of characters python
  16. turn string into variable name python
  17. turn string into json object
  18. turn string into char
  19. turn string into array ruby
  20. turn string into integer java

I am fetching data from the server and getting the data successfully as you can see in the screenshot, I also checked my code. my code is working properly when I take data manually, but I don't know ...

I am fetching data from the server and getting the data successfully as you can see in the screenshot, I also checked my code. my code is working properly when I take data manually, but I don't know ...

  1. typeerror cannot read property
  2. typeerror cannot read property 'map' of undefined
  3. typeerror cannot read property 'length' of undefined
  4. typeerror cannot read property 'push' of undefined
  5. typeerror cannot read property 'setstate' of undefined
  6. typeerror cannot read property '0' of undefined
  7. typeerror cannot read property 'then' of undefined
  8. typeerror cannot read property 'location' of undefined
  9. typeerror cannot read property 'apply' of undefined
  10. typeerror cannot read property 'requestcontent' of undefined
  11. typeerror cannot read property 'contexttypes' of undefined
  12. typeerror cannot read property 'state' of undefined
  13. typeerror cannot read property 'createelement' of undefined
  14. typeerror cannot read property 'props' of undefined
  15. typeerror cannot read property 'kind' of undefined
  16. typeerror cannot read property 'get' of undefined
  17. typeerror cannot read property 'prototype' of undefined
  18. typeerror cannot read property 'subscribe' of undefined
  19. typeerror cannot read property 'foreach' of undefined
  20. typeerror cannot read property 'name' of undefined

As suggested here, I should use canvas renderer so that images can be displayed in nodes. First of all I create a graphClass: graphClass.g = { nodes: [], edges: [] } Then, I create each node:...

As suggested here, I should use canvas renderer so that images can be displayed in nodes. First of all I create a graphClass: graphClass.g = { nodes: [], edges: [] } Then, I create each node:...