2

I have a JavaScript associative array, say:

var myArray = new Object();
myArray["id1"] = "completed";
myArray["id2"] = "notCompleted";
myArray["id3"] = "started";

How can I update value of each item in this array, so that output should be,

myArray["id1"] = "newValue";
myArray["id2"] = "newValue";
myArray["id3"] = "newValue";
1
  • Just do what you did in the first code block of your question. A loop block will help you. Commented Aug 10, 2016 at 8:40

2 Answers 2

4

Get all property name array using Object.keys() and then iterate over them update property value using Array#forEach method.

var myArray = new Object();
myArray["id1"] = "completed";
myArray["id2"] = "notCompleted";
myArray["id3"] = "started";

Object.keys(myArray).forEach(function(k) {
  myArray[k] = 'newVal';
});

console.log(myArray);


Faster way using a while loop.

var myArray = new Object();
myArray["id1"] = "completed";
myArray["id2"] = "notCompleted";
myArray["id3"] = "started";

var names = Object.keys(myArray),
  i = names.length;

while (i--) {
  myArray[names[i]] = 'newVal';
};

console.log(myArray);

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

5 Comments

Should i prefer this answer over for (var prop in myArray)... if yes why?
@A.R. forEach is compatible for IE9+. Check developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. Whereas for-in is compatible with IE6+. So in case you are looking to support for old browsers, you might want prefer stackoverflow.com/a/38868298/5102631 Or else this is fine. No as such difference. You can attach polyfill anytime, although native levels code are faster. I think @Pranav agrees to this!
@Ayan what do you mean by 'You can attach polyfill anytime'? Can you please explain?
@A.R. As we are speaking of the compatible issues for forEach Check this mdn link - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. Scroll down, you would get a pollyfill. A pollyfill is a substitute code to make the feature compatible in the browsers which natively dont support it. Like in an IE8 browser, you can use this code.
@A.R. : forEach is much slower one..... but you can use while loop to make it faster....... there may be browser compatibility issues with Object.keys().... check here : developer.mozilla.org/en/docs/Web/JavaScript/Reference/… , For implementing polyfill option for older browser : developer.mozilla.org/en/docs/Web/JavaScript/Reference/… ..... For the comparison refer : stackoverflow.com/questions/25052758/…
2
  • Use for in loop
  • Its always advisable to use hasOwnProperty to iterate across only the non-inherited properties.

var myArray = new Object();
myArray["id1"] = "completed";
myArray["id2"] = "notCompleted";
myArray["id3"] = "started";

for (var prop in myArray) {
  if (myArray.hasOwnProperty(prop)) {
    myArray[prop] = 'newValue';
  }
}

console.log(myArray);

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.