Home >Web Front-end >Front-end Q&A >How to replace specified elements in an array in es6

How to replace specified elements in an array in es6

青灯夜游
青灯夜游Original
2022-11-16 17:57:303909browse

3 replacement methods: 1. Use bracket notation to reassign the value, the syntax "arr[index] = 'new element value';"; 2. Use Array.splice() to replace, the syntax "arr.splice (index, 1, 'new element value');"; 3. Use a for loop to replace, the syntax "for(i=0;i

How to replace specified elements in an array in es6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

Method 1: Use bracket notation reassignment to replace array elements

Implementation idea:

  • Use the indexOf() method to get the index of the element.

  • Use bracket notation to change the value of an element at a specific index.

  • The values ​​of the array elements will be updated in place.

Example:

const arr = ['a', 'b', 'c'];
const index = arr.indexOf('a'); // ?️  0
if (index !== -1) {
  arr[index] = 'z';
}
console.log(arr); // ?️ ['z', 'b', 'c']

How to replace specified elements in an array in es6

We use the Array.indexOf() method to get the index of the array element whose value is a index.

Then we replace the element at that index with the new value.

Please note that if the indexOf method does not find an element with the provided value, it returns -1. We check that the method does not return an index of -1 to ensure that there is an element with the specified value.

Indices in JavaScript are zero-based, so the first element has an index of 0 and the last element has an index of arr.length - 1.

Alternatively, we can use the array.splice() method.

Method 2: Use Array.splice() to replace elements in the array

Implementation idea:

  • Use the indexOf() method to get the index of the element to be replaced.

  • Call the Array.splice() method to replace the element at a specific index.

  • The array elements will be replaced in place.

Example:

const arr = ['a', 'b', 'c'];
console.log(arr); // ?️ ['a', 'b', 'c']
const index = arr.indexOf('b'); // ?️  1
arr.splice(index, 1, 'z');
console.log(arr); // ?️ ['a', 'z', 'c']

How to replace specified elements in an array in es6

We pass the following 3 parameters to the Array.splice() method:

  • start index - Start changing the index of the array.

  • delete count - How many elements should be deleted from the array.

  • item1 - The item to add to the array.

We set the start index to the index of the array element to be replaced.

We set the deletion count to 1, so the Array.splice() method will delete the array element at the specified index and add the provided third argument to the same index.

In practice, we delete the array element at the specified index and then insert a different value at the same index, so we end up replacing the array element.

Another approach is to use a basic for loop. **

Method 3: Use for loop to replace elements in the array

Implementation ideas:

  • Use a for loop to iterate array.length times.

  • On each iteration, check whether the array element is the element to be replaced.

  • If the condition is met, replace the element at index and break out of the for loop.

Example:

const arr = ['a', 'b', 'c'];
console.log(arr); // ?️ ['a', 'b', 'c']
for (let index = 0; index < arr.length; index++) {
  if (arr[index] === &#39;c&#39;) {
    arr[index] = &#39;z&#39;;
    break;
  }
}
console.log(arr); // ?️ [&#39;a&#39;, &#39;b&#39;, &#39;z&#39;]

How to replace specified elements in an array in es6

We used a basic for loop to iterate over the array. In each iteration, we check if the element is the one we want to replace.

Once we find and replace the element, we break out of the loop to avoid unnecessary work.

If you want to replace all array elements with a specific value, just remove the break statement.

[Recommended learning: javascript video tutorial]

The above is the detailed content of How to replace specified elements in an array in es6. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn