Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make array with string list

Tags:

javascript

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('-'));
like image 524
Fury Avatar asked Aug 04 '26 13:08

Fury


2 Answers

You could use the following:

'196-1526, 85-651, 197-1519'.replace(/-\d+(,|$)/g, '').split(/\s/)
like image 64
jabclab Avatar answered Aug 07 '26 04:08

jabclab


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()


like image 31
Nope Avatar answered Aug 07 '26 04:08

Nope