Check if array contains specific character

4

I need to check if an array has a specific character (this: |). If it contains, this character must be deleted, and the next word that would be after that character (which actually is a separator of items) would be added in the next index. That is, the array can not have in its content the character mentioned above. If there is, delete it and add in the array the word that would be after that character.

The attached image reflects the problem. I need to resolve with javascript / jquery. ArrayexamplewiththespecificcharacterwhereIneedtodeleteitandaddtheitem"Male Cut" in the next index, which would be in the case of this example: 6.

    
asked by anonymous 14.12.2018 / 03:05

3 answers

10

If you want to change only the original array without creating a new one, and still compatible with all browsers, including IE 11 (a arrow function item => no forEach is not supported in IE 11, without taking the merits of the response of vnbrs ):

var array = ["Escova|Corte Masculino", "Gel", "Cabelo|Corte Masculino", "Escova", "Gel", "Gel|Corte Masculino"];

for(var x=0; x<array.length; x++){
   var i = array[x];
   var idx = i.indexOf("|");
   if(~idx){
      array[x] = i.substr(idx+1);
      array.splice(x, 0, i.substr(0, idx));
   }
}

console.log(array);
    
14.12.2018 / 05:35
7

You can use a loop, combined with Array.concat and String.split to separate the items with the pipe.

const arrayOriginal = ["Tomate", "Abacaxi", "Carne|Fritas"];
let novoArray = [];

arrayOriginal.forEach(item => {
  novoArray = novoArray.concat(item.split("|"));
});

The result is

["Tomate", "Abacaxi", "Carne", "Fritas"]
    
14.12.2018 / 03:50
2

Personal Talk:)

Just adding here a way to do with jquery, it also uses regular expression, but it can help a lot in the task of not creating a new Array 0 /

var teste = ['Escova|Corte Masculino','Gel','Cabelo|Corte Masculino']; // Array com os dados
teste = teste.toString().replace('|',',').split(','); 

The above code is using 3 jQuery functions.

  • toString() - converts array to string and separates array positions by "comma" Example: 'Brush | Cut, Male, Gel, Hair | Male Cut'
  • .replace('|',',') - this function as the name already says Replaces one value with the other, that is, the first parameter by the second. In this case, the "|" by ","
  • .split(',') - and finally the SPLIT function divides the String from the passed parameter, which in turn is , .

At the end the step by step is:

  • Get the array and convert it to string (separating the positions with comma)
  • Replace the | (pipes) per comma.
  • Convert the string back into Array by dividing it every time you find a comma.
  

The big problem with this solution is that it only applies replace on the first occurrence it encounters, that is, if the array contains more than one element with | it will only replace the first. (boring, right?)

But relax, which is the regular expression to help.

.replace(/\|/g,',')

Using the regular expression causes replace to replace all occurrences of the pipe character ( | ) within the string.

If you are not familiar with the regular expression, here is some good study material: I want to learn

Anyway, the end result of the method is:

var teste = ['Escova|Corte Masculino','Gel','Cabelo|Barba']; // Array com os dados
teste = teste.toString().replace(/\|/g,',').split(','); 

I hope I have helped:)

    
14.12.2018 / 13:08