-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?

3
  • arr = "1,4,3,2,4,2,4";var answ = arr.split(',');console.log(answ);. very simple Commented Apr 12, 2017 at 7:17
  • what should parseInt do in this context? getting a number or getting an integer number? Commented Apr 12, 2017 at 7:18
  • globArr = []; var arr = "1,4,3,2,4,2,4"; var answ = arr.split(','); for(var i = 0; i < answ.length; i++) { globArr.push(parseInt(answ[i])); console.log(answ[i]); } console.log(globArr); Commented Apr 12, 2017 at 7:27

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.