0

I am having issues with this code:

var openingSegments = Array(7).fill([]);
openingSegments[0].push({'start': '1100', 'end': '1900'});

Because when I do:

console.log(openingSegments);

It gives me :

[ [ { start: '1100', end: '1900' } ],
  [ { start: '1100', end: '1900' } ],
  [ { start: '1100', end: '1900' } ],
  [ { start: '1100', end: '1900' } ],
  [ { start: '1100', end: '1900' } ],
  [ { start: '1100', end: '1900' } ],
  [ { start: '1100', end: '1900' } ] ]

As if I had pushed all the elements of 'hoursOfDay' which is not the case since i only applied push on the very first element (index 0).

What is happening here ?

2 Answers 2

3

fill just copies the passed argument across all items. Actually you have only one array object and 7 reference copies of that object pushed into the array. Changing array via one reference affects all items.

Instead of fill you can use Array#from function with the mapFn parameter of it.

var openingSegments = Array.from({ length: 7 }, x => []);
openingSegments[0].push({'start': '1100', 'end': '1900'});

console.log(openingSegments[0]);
console.log(openingSegments[1]);

Sign up to request clarification or add additional context in comments.

1 Comment

ok this is really much appreciated ! So now should I take down this answer because it wasn't the right question or ?
0

Array.fill just fills your array with the exact same objects.

This is basically how fill works:

function fillFunc(size, element) {
  const resultArray = [];
  for (let i = 0; i < size; ++i) {
    resultArray.push(element);
  }
  return resultArray;
}
const filledArray = fillFunc(7, []);

As you can see, the resulting array just gets filled with references to the initial object. I'd go with something other than Array.fill() for your particular needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.