-1

I have a variable that contains a few comma seperated values. I'd like to split these up and push them into an array as integers only I can't figure it out.

I've the following...

globArr = [];
arr = "1,4,3,2,4,2,4";

var answ = arr.split(',');
globArr.push(parseInt(answ));

console.log(globArr);

https://jsfiddle.net/d7hke7gq/

Can anybody tell me where im going wrong?

Liam
  • 9,725
  • 39
  • 111
  • 209

2 Answers2

2

You need to iterate over answ array for pushing each value:

let globArr = [];
let arr = "1,2,3,4,5,6,7,8,9,10";
let answ = arr.split(',');
answ.forEach(function(obj){
      globArr.push(parseInt(obj,10));
});
console.log(globArr);
DarckBlezzer
  • 4,578
  • 1
  • 41
  • 51
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

Here you go:

Updated fiddle

globArr = [];
arr = "1,4,3,2,4,2,4";

var answ = arr.split(',');
$.each(answ, function(){
globArr.push(parseInt(this));
});


console.log(globArr);
Matt Murdock
  • 159
  • 1
  • 2
  • 11