With the question explained, you can do it with map, as @DevJoel has already said.
And, in the case of a arrayI think it is better to use jointhan toString.
The method join()joins all the elements of an array into a string. The default separator is the comma ,, and it gives you the possibility to use another separator if necessary. Also, if an element is undefinedor nullis converted to an empty string.
For other separators, see examples in the documentation link.
Here is a simplified way:
var act = [10002197,10001755,10001087,10001879,3508477478,10001881];
var posiciones = [0,2,3] // el debera imprimir (10002197,10001087,10001879)
var arrResultado = posiciones.map(i => act[i]);
document.write(arrResultado.join());
Using the toString() method you could achieve this result, (answer adapted to your comment code).
var array1= [100,200,300,400,500,600,700,800,900];
var array2=[1,2,5];
var posit = array2.map(function(el){
return array1[el];
});
console.log(posit.toString()); // uso del método
With the question explained, you can do it with
map
, as @DevJoel has already said.And, in the case of a
array
I think it is better to usejoin
thantoString
.The method
join()
joins all the elements of an array into a string. The default separator is the comma,
, and it gives you the possibility to use another separator if necessary. Also, if an element isundefined
ornull
is converted to an empty string.For other separators, see examples in the documentation link.
Here is a simplified way:
Using the toString() method you could achieve this result, (answer adapted to your comment code).
the best and fastest option is
will return the elements of the array in a string separated by a comma, if you want a different separation specify it within the join "
join(";")
"