How to change elements inside and array

How can I replace the elements "e" inside "arr" with change0 ?

The arr array will be the user, and I need to change it, there is no way to predict which element will be "e" .

 var arr = [ "a", "b", "c", "d", "e", "f", "g", "h", "e", "j", "e"]; var change0 = 2 var change1 = 1 document.write(arr); 
+5
source share
8 answers

Using forEach :

 var arr = [ "a", "b", "c", "d", "e", "f", "g", "h", "e", "j", "e"]; var change = "changed"; arr.forEach(function(v, i) { if(v === "e") arr[i] = change; }); console.log(arr); 
+1
source

You can use the map() method and this will return a new updated array and keep the original.

 var arr = [ "a", "b", "c", "d", "e", "f", "g", "h", "e", "j", "e"]; var change0 = 2; var result = arr.map(e => e == 'e' ? change0 : e); console.log(result) 
+2
source

You can do this with the join and split methods.

 var replace="change0"; var arr = [ "a", "b", "c", "d", "e", "f", "g", "h", "e", "j", "e"]; console.log(arr.join().split('e').join(replace).split(',')); 
+2
source

Run a loop to get the index of the element "e", and then repeat until there are more elements left:

 var arr = [ "a", "b", "c", "d", "e", "f", "g", "h", "e", "j", "e"]; while (arr.indexOf("e") > 0){ var index = arr.indexOf("e"); arr[index] = "change0"; } document.write(arr); 
+1
source

You can use Array#indexOf and look for all the elements and change then with the given value.

 var array = [ "a", "b", "c", "d", "e", "f", "g", "h", "e", "j", "e"], search = "e", change = 2, p = array.indexOf(search); while (p !== -1) { array[p] = change; p = array.indexOf(search, p + 1); } console.log(array); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 
+1
source

You can use indexOf to determine the index of an element in an array. So you can just like that:

 arr[arr.indexOf('e')] = change0; 

This will not work if you have multiple elements with 'e' values. It will only change the first one, so you have to go through the cycle. Or use a card.

+1
source
  • Convert to string
  • Replace 'e'
  • Convert back to array

 var arr = [ "a", "b", "c", "d", "e", "f", "g", "h", "e", "j", "e"]; console.log((((arr.toString()).replace(/e/g,"changed"))).split(',')); 
+1
source

If you intend to process more than one letter, it is best to prepare an encoder / decoder (function or array) in advance. Then your work becomes as simple as:

 output = input.map(val => encoder[val] || val); 

Part || val || val is for values ​​not processed by the encoder.

Working example

In this example, we use split("") to convert the string to an array and join("") to do the opposite.

 PrepareCodec: var Latin = "abcdefghijklmnoprstuvyzABCDEFGHIJKLMNOPRSTUVYZ"; var Cyrillic = ""; var encoder = {}, decoder = {}; for (let i in Latin) encoder[Latin[i]] = Cyrillic[i]; for (let i in Cyrillic) decoder[Cyrillic[i]] = Latin[i]; EncryptionTest: var src = prompt("Enter text to encrypt:", "Hello, world!"); var enc = src.split("").map(val => encoder[val] || val).join(""); DecryptionTest: enc = prompt("Enter text to decrypt:", enc); var dec = enc.split("").map(val => decoder[val] || val).join(""); FinalResult: prompt("Decrypted text:", dec); 
0
source

Source: https://habr.com/ru/post/1266785/


All Articles