How to replace an item in an array with JavaScript?

后端 未结 26 2163
执笔经年
执笔经年 2020-11-29 15:33

Each item of this array is some number.

var items = Array(523,3452,334,31, ...5346);

How do I replace some number in with array with a new on

相关标签:
26条回答
  • 2020-11-29 16:00

    I solved this problem using for loops and iterating through the original array and adding the positions of the matching arreas to another array and then looping through that array and changing it in the original array then return it, I used and arrow function but a regular function would work too.

    var replace = (arr, replaceThis, WithThis) => {
        if (!Array.isArray(arr)) throw new RangeError("Error");
        var itemSpots = [];
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == replaceThis) itemSpots.push(i);
        }
    
        for (var i = 0; i < itemSpots.length; i++) {
            arr[itemSpots[i]] = WithThis;
        }
    
        return arr;
    };
    
    0 讨论(0)
  • 2020-11-29 16:00
     items[items.indexOf(3452)] = 1010
    

    great for simple swaps. try the snippet below

    const items = Array(523, 3452, 334, 31, 5346);
    console.log(items)
    
    items[items.indexOf(3452)] = 1010
    console.log(items)

    0 讨论(0)
  • 2020-11-29 16:01

    Answer from @gilly3 is great.

    How to extend this for array of objects

    I prefer the following way to update the new updated record into my array of records when I get data from the server. It keeps the order intact and quite straight forward one liner.

    users = users.map(u => u.id !== editedUser.id ? u : editedUser);

    var users = [
    {id: 1, firstname: 'John', lastname: 'Sena'},
    {id: 2, firstname: 'Serena', lastname: 'Wilham'},
    {id: 3, firstname: 'William', lastname: 'Cook'}
    ];
    
    var editedUser = {id: 2, firstname: 'Big Serena', lastname: 'William'};
    
    users = users.map(u => u.id !== editedUser.id ? u : editedUser);
    
    console.log('users -> ', users);

    0 讨论(0)
  • 2020-11-29 16:02

    The easiest way is to use some libraries like underscorejs and map method.

    var items = Array(523,3452,334,31,...5346);
    
    _.map(items, function(num) {
      return (num == 3452) ? 1010 : num; 
    });
    => [523, 1010, 334, 31, ...5346]
    
    0 讨论(0)
  • 2020-11-29 16:02
    var index = Array.indexOf(Array value);
            if (index > -1) {
              Array.splice(index, 1);
            }
    

    from here you can delete a particular value from array and based on the same index you can insert value in array .

     Array.splice(index, 0, Array value);
    
    0 讨论(0)
  • 2020-11-29 16:02

    First, rewrite your array like this:

    var items = [523,3452,334,31,...5346];
    

    Next, access the element in the array through its index number. The formula to determine the index number is: n-1

    To replace the first item (n=1) in the array, write:

    items[0] = Enter Your New Number;
    

    In your example, the number 3452 is in the second position (n=2). So the formula to determine the index number is 2-1 = 1. So write the following code to replace 3452 with 1010:

    items[1] = 1010;
    
    0 讨论(0)
提交回复
热议问题