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.
This question is related to
javascript
jquery
So I had a problem I needed solved. I had an array object with values. One of those values I needed to update if the value == X.I needed X value to be updated to the Y value. Looking over examples here none of them worked for what I needed or wanted. I finally figured out a simple solution to the problem and was actually surprised it worked. Now normally I like to put the full code solution into these answers but due to its complexity I wont do that here. If anyone finds they cant make this solution work or need more code let me know and I will attempt to update this at some later date to help. For the most part if the array object has named values this solution should work.
$scope.model.ticketsArr.forEach(function (Ticket) {
if (Ticket.AppointmentType == 'CRASH_TECH_SUPPORT') {
Ticket.AppointmentType = '360_SUPPORT'
}
});
Full example below _____________________________________________________
var Students = [
{ ID: 1, FName: "Ajay", LName: "Test1", Age: 20 },
{ ID: 2, FName: "Jack", LName: "Test2", Age: 21 },
{ ID: 3, FName: "John", LName: "Test3", age: 22 },
{ ID: 4, FName: "Steve", LName: "Test4", Age: 22 }
]
Students.forEach(function (Student) {
if (Student.LName == 'Test1') {
Student.LName = 'Smith'
}
if (Student.LName == 'Test2') {
Student.LName = 'Black'
}
});
Students.forEach(function (Student) {
document.write(Student.FName + " " + Student.LName + "<BR>");
});
How about;
function keyValue(key, value){
this.Key = key;
this.Value = value;
};
keyValue.prototype.updateTo = function(newKey, newValue) {
this.Key = newKey;
this.Value = newValue;
};
array[1].updateTo("xxx", "999");
Why not use an object1?
var dict = { "a": 1, "b": 2, "c": 3 };
Then you can update it like so
dict.a = 23;
or
dict["a"] = 23;
If you wan't to delete2 a particular key, it's as simple as:
delete dict.a;
1 See Objects vs arrays in Javascript for key/value pairs.
2 See the delete
operator.
If you want to reassign an element in an array, you can do the following:
var blah = ['Jan', 'Fed', 'Apr'];
console.log(blah);
function reassign(array, index, newValue) {
array[index] = newValue;
return array;
}
reassign(blah, [2], 'Mar');
function Update(key, value)
{
for (var i = 0; i < array.length; i++) {
if (array[i].Key == key) {
array[i].Value = value;
break;
}
}
}
Source: Stackoverflow.com