How to update array value javascript?
javascript, jquery
Solution
"But i want to know a better way to do this, if there is one ?"
Yes, since you seem to already have the original object, there's no reason to fetch it again from the Array.
function Update(keyValue, newKey, newValue)
{
keyValue.Key = newKey;
keyValue.Value = newValue;
}
Problem
I have an array of 3 objects of keyValue constructor in javacsript: ``` function keyValue(key, value){ this.Key = key; this.Value = value; }; var array = []; array.push(new keyValue("a","1"),new keyValue("b","2"),new keyValue("c","3")); ``` I also have a function 'Update' which takes `keyValue object as parameter` and updates the value of that object in the array: ``` function Update(keyValue, newKey, newValue) { //Now my question comes here, i got keyValue object here which i have to //update in the array i know 1 way to do this var index = array.indexOf(keyValue); array[index].Key = newKey; array[index].Value = newValue; } ``` But I want a better way to do this if there is one.