Skip to content
Advertisement

How to update the 1st value of the sub list with another list

I want to add the 1st element of each sublist with the values of another list

JavaScript

Output (comming):

JavaScript

Output (want):

JavaScript

Advertisement

Answer

Here let me tell you what is happening in your code…

JavaScript

In this line you made a nested list in which all the sublists are pointing to the same sublist in memory.

JavaScript

In this line you now made your first sublist to point to a newly created list in memory. So thats why in the output the first list is same and not affected by the loop.

JavaScript

Here all the other sublists (1,5) are being edited because they are pointing to the same elements.

So the solution would be: To declare your list like this :

JavaScript

This way each sublist is pointing to a different sublist. Hope this answer helps.

Happy coding!

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement