Replacing an element in an object array
javascript
Solution
The correct way is
array[0] = {name: "name3"};
Your existing code does not work as expected because you are taking a referenc* to the first element with
var element = array[0];
and then you are replacing the value of this local variable with another object. This leaves the original array unmodified.
Problem
I want to replace the entire object in an array. http://jsfiddle.net/CWSbJ/ ``` var array = [ {name: "name1" }, { name: "name2" } ]; var element = array[0]; element = {name: "name3"}; alert(array[0].name); ``` In this piece of code I would expect the output name3, why can't I replace an entire object in an array like this? And what is the good way to do this?