Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update array value javascript?

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.

like image 931
Tom Rider Avatar asked Oct 25 '12 13:10

Tom Rider


People also ask

How do you update values in an array?

To update all the elements of an array, call the forEach() method on the array, passing it a function. The function gets called for each element in the array and allows us to update the array's values. Copied! const arr = ['zero', 'one', 'two']; arr.

How do I change the value of an array in react?

To update an object in an array in React state: Use the map() method to iterate over the array. On each iteration, check if a certain condition is met. Update the object that satisfies the condition and return all other objects as is.


1 Answers

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>");
    });
like image 189
Deathstalker Avatar answered Sep 20 '22 12:09

Deathstalker