javascript - cannot iterate through array and change value in JS -


I have to repeat through an array, change one of its values, and change the changes to a Another is to make an order.

This is what I have done so far:

JS:

  var arr = new array (); Arr ['t1'] = "sdfsdf"; Arr ['t2'] = "sdfsdf"; ARR ['T3'] = "SDFDDF"; Arr ['t4'] = "sdfsdf"; Arr ['t5'] = "sdfsdf"; Var final = new array (); For (var i = 0; i & lt; = 5; i ++) {AR ['T2'] = I; Last.push (Arrival); } Console.log (last);  

Unfortunately, these are my results

image Enter Details Here

As you can see, I'm not getting the result as 0,1,2. Instead, I'm receiving 2, 2, 2. What is my outcome?

Enter image details here

< P> How can I fix it?

You have to copy, otherwise you are working in the context of the same object all the time. As previously mentioned - Javascript does not have an associate array, only properties with properties.

  var arr = {}; // empty object arr ['t1'] = "sdfsdf"; Arr ['t2'] = "sdfsdf"; ARR ['T3'] = "SDFDDF"; Arr ['t4'] = "sdfsdf"; Arr ['t5'] = "sdfsdf"; Var final = new array (); (Var i = 0; i & lt; = 5; i ++) for (var copy = JSON.parse (JSON.stringify (arr)); // copy, copy [[T2] ] = I; // Set the value of its element to Value Poiss (Copy); // Copy console.log to the last console (last);  

ps: 'T1']

instead of dot notation arr.t1 >

Comments

Popular posts from this blog

c# - SignalR: "Protocol error: Unknown transport." when navigating to hub -

c# - WPF Expander overlay used in ItemsControl -

class - Kivy: how to instantiate a dynamic classes in python -