fetch first element of every string in array using javascript [closed]

Use a forEach to loop through the array and split using ‘,’.

var arr = ["ds", "1:0,1,2,3,4", "2:0,2,3,4,5", "3:0,6,7,8,9"];
var newArr = [];
arr.forEach(function(el,index) {
  if(index!=0)
  newArr.push(el.split(',')[0]);
});
console.log(newArr)

Leave a Comment