How to Replace an Array's Element

How to replace item in array?

var index = items.indexOf(3452);

if (index !== -1) {
items[index] = 1010;
}

Also it is recommend you not use the constructor method to initialize your arrays. Instead, use the literal syntax:

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

You can also use the ~ operator if you are into terse JavaScript and want to shorten the -1 comparison:

var index = items.indexOf(3452);

if (~index) {
items[index] = 1010;
}

Sometimes I even like to write a contains function to abstract this check and make it easier to understand what's going on. What's awesome is this works on arrays and strings both:

var contains = function (haystack, needle) {
return !!~haystack.indexOf(needle);
};

// can be used like so now:
if (contains(items, 3452)) {
// do something else...
}

Starting with ES6/ES2015 for strings, and proposed for ES2016 for arrays, you can more easily determine if a source contains another value:

if (haystack.includes(needle)) {
// do your thing
}

Javascript - Replace multiple elements in an array using index

Use Array.fill()

var array1 = ['S', 'T', 'A', 'C', 'K', 'O', 'V', 'E', 'R', 'F', 'L', 'O', 'W'];

array1.fill('X', 3, 10)

console.log(array1)

Replacing certain elements of an array based on a given index

you can do it with list comprehension, which will save you some code lines and make it more interpretable, though it won't improve the runtime, as it uses loops under the hood. Also note that by incorparating a varying length lists, you'll loose any runtime improvements of the NumPy library, as to do so it is being cast to dtype=object

Arr1 = np.array([9,7,3,1], dtype=object)

Arr2 = np.array([[14,6], [1], [13,2]], dtype=object)

Arr3 = np.array([0,2])

result = np.array([[Arr1[i]] if not np.sum(Arr3 == i) else Arr2[i] for i in np.arange(Arr1.size)], dtype=object)

result
OUTPUT: array([list([14, 6]), list([7]), list([13, 2]), list([1])], dtype=object)

Cheers

How to replace array element with respect to Index on another array

I don't think there is any shorthand for such a verbose question.
Something on top of my mind is

array2.forEach((el) => {
array1[el[0]] = el[1];
})

How to replace or update item of array in JavaScript

You might have misused the slice method, change slice to splice works for me:

this.items.splice(index, 1, item)

slice doesn't trigger view updates according to the documentation here.

Vue wraps an observed array’s mutation methods so they will also
trigger view updates. The wrapped methods are:

  • push()
  • pop()
  • shift()
  • unshift()
  • splice()
  • sort()
  • reverse()

Remove an element from array and replace with 0?

Here is an approach, similar to what I described in the comments above.

import java.util.Arrays;
public class main
{
// tip: arguments are passed via the field below this editor
public static void main(String[] args)
{
int[] arr = {3, 5, 7, 8, 5, 12, 2};
remove(arr, 5);
System.out.println(Arrays.toString(arr)); // [3, 7, 8, 5, 12, 2, 0]
}

public static void remove(int[] arr, int toRemove) {
int idx = -1;
// determine first occurrence of toRemove
for(int i = 0; i < arr.length; i++) {
if(arr[i] == toRemove) {
idx = i;
break;
}
}
// if not found, return
if(idx == -1) return;
// shift other elements down
for(int i = idx; i < arr.length-1; i++) {
arr[i] = arr[i+1];
}
// set last element to 0
arr[arr.length-1] = 0;
}
}

How to replace an element in an array without changing the other elements?

Why not assign directly?

const
months = ['Jan', 'March', 'April', 'June'];

months[2] = 'newVal';

console.log(months);

Replace elements of an array with elements of another array based on a condition

We can use keyed collection - Map to get items by key.

const array1 = ["1", "2", "3"]
const array2 = [{"name": "a", "id" : "1"}, {"name": "b", "id" : "2"}, {"name": "c", "id" : "3"}, {"name": "c", "id" : "4"}]
const array2Map = new Map(array2.map(o=> [o.id, o]));
const result = array1.map(o => ({ ...array2Map.get(o)}));console.log(result);


Related Topics



Leave a reply



Submit