Split in reverse order

Asked

Viewed 99 times

0

var itens: "101|102|103|104";

var quebra = itens.split("|");
    for (var i = quebra.length - 1; i >= 0; i--) {
    var unidade = quebra[i];

$("#lista").append("<p>" +getValue(unidade, jsonTitles)+"</p>");
}

the result of this is as follows: 104 103 102 101

would need it to be 101 102 103 104

2 answers

4

Just use the method re-verse.

var itens = "101|102|103|104";

var quebra = itens.split("|").reverse(); // inverte o array
for (var i = quebra.length - 1; i >= 0; i--) {
  var unidade = quebra[i];
  console.log(unidade);
}

  • both worked... thanks Aerte

  • Sort, the array object has several interesting methods. ;)

3

Add .sort(function (a, b) { return b - a; }) shortly after the itens.split("|").

  • worked perfectly.. alias that way most exit options. thank you

  • Thanks! Whenever you need us we’ll be here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.