I Have array like this.
var array= ["4220495|1", "4220495|2"]
I want these values in separate array like this
var firstArray=["4220495", "4220495"];
var secondArray=["1", "2"];
If you want more information let me know.
You can iterate over the values and get the result
var array= ["4220495|1", "4220495|2"];
var firstArray = [];
var secondArray = [];
for (var i = 0; i < array.length;i++) {
var itemArray = array[i].split('|');
firstArray.push(itemArray[0]);
secondArray.push(itemArray[1])
}
console.log(firstArray);
console.log(secondArray);
Many ways to do this. Here's one:
var array= ["4220495|1", "4220495|2"];
var firstArray = array.map(function(v){ return v.split("|")[0] });
var secondArray= array.map(function(v){ return v.split("|")[1] });
This is a more generic solution for any split length.
var array = ["4220495|1", "4220495|2"],
firstArray = [],
secondArray = [];
function arraySplit(array, target) {
array.forEach(function (a) {
a.split('|').forEach(function (b, i) {
target[i].push(b);
});
});
}
arraySplit(array, [firstArray, secondArray]);
document.write('<pre>' + JSON.stringify(firstArray, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(secondArray, 0, 4) + '</pre>');