0

I am attempting to create an array of dictionaries and then to iteratively update individual values in those dictionaries using Array.map(). It appears that every iteration, despite my referencing each individual dictionary by its index in the array, all dictionaries are being updated.

Here's a snippet that shows this behavior:

testarray = Array(5).fill({'number': 0});
testarray.map(function(element, index) {
    testarray[index]['number'] = Math.random()
});

I'm a major greenhorn in Javascript and may be thinking too much in Python, but is there a way to circumvent this behavior, or to better accomplish my goal of creating a list of dictionaries where the individual values of each dictionary can be updated independently?

Edit: I'm adding an example to show that this behavior also applies when grabbing elements from other lists to insert into the list of dictionaries:

letterarray = ['a', 'b', 'c', 'd', 'e'];
testarray = Array(5).fill({'letter': 'x', 'number': 0});
testarray.map(function(element, index) {
    testarray[index]['letter'] = letterarray[index]
    testarray[index]['number'] = Math.random()
});

This ultimately returns:

[ { letter: 'e', number: 0.7262187794379416 },
  { letter: 'e', number: 0.7262187794379416 },
  { letter: 'e', number: 0.7262187794379416 },
  { letter: 'e', number: 0.7262187794379416 },
  { letter: 'e', number: 0.7262187794379416 } ]

As opposed to:

[ { letter: 'a', number: 0.7262187794379416 },
  { letter: 'b', number: 0.9870744758968584 },
  { letter: 'c', number: 0.16493191974189658 },
  { letter: 'd', number: 0.6403787941360672 },
  { letter: 'e', number: 0.49447691255595005 } ]

which is the desired behavior.

  • `Array.from( { length: 4 }, () => ({ number: Math.random() }) );` – CertainPerformance Feb 18 '19 at 03:45
  • Thanks, @CertainPerformance. These definitely work for this specific use case, but if I have values I'm porting in from elsewhere (from another list for example), I'm not sure how these would apply. Is there no way to define the list of dictionaries and then update it later? – Colin Conwell Feb 18 '19 at 04:02
  • in the question, the issue can be resolved by changing the map to forEach and removing index and accessing tesarray[index]['number'] to element.number. But my doubt is since it's in a forEach loop will the Math.random() generates a random number. @CertainPerformance any idea on this doubt ? – Learner Feb 18 '19 at 04:03
  • You can always zip them together later, but without seeing an example, it's hard to say what sort of functionality you're looking for exactly – CertainPerformance Feb 18 '19 at 04:03
  • @CertainPerformance but when I check the testarray each number property had the same value. Is it because Math.random was called inside forEach and it was not able to generate a new number? – Learner Feb 18 '19 at 04:12
  • @CertainPerformance I had a look at the post you flagged as a potential duplicate, but I think this is a different question, since I'm not simply trying to make an array of four dictionaries with random numbers in each. I've updated my question to reflect that. – Colin Conwell Feb 18 '19 at 04:14
  • It's still almost exactly the same - you're `.fill`ing with objects, which means that the array is composed of 5 references to the same object in memory. As the linked post shows, don't `.fill` with objects, use `Array.from` instead – CertainPerformance Feb 18 '19 at 04:16
  • @CertainPerformance That did the trick. Thanks very much. – Colin Conwell Feb 18 '19 at 05:12

0 Answers0