I have a generated list which is like this
196-1526, 85-651, 197-1519
I need the array like this. Each node has two part. I need only the first part of each node in one array.
196, 85, 197
I already have this code which generage 196
str.substr(0,str.indexOf('-'));
You could use the following:
'196-1526, 85-651, 197-1519'.replace(/-\d+(,|$)/g, '').split(/\s/)
If the input is a string you can use split() and push(), similar to this:
var x = "196-1526, 85-651, 197-1519"
var y = x.split(',');
var myArray = [];
for(i = 0; i < y.length; i++){
myArray.push(y[i].split('-')[0].trim());
}
DEMO - Using split() and push()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With