Could someone help me with this javascript. I have two variables, with commaseparated words.
var names = "Verwerkende industrie, Retail, Primaire producent, Out of home, Groothandel, Grondstof, Consument, Bewerkende industrie";
var numbers = "9, 3, 4, 2, 7, 9, 3, 2";
I would like to combine them into an array that should end up looking like this
var combined = [
['Verwerkende industrie', 9],
['Retail', 3],
['Primaire producent', 4],
['Out of home', 2],
['Groothandel', 7],
['Grondstof', 9],
['Consument', 3],
['Bewerkende industrie', 2]
];
asked Apr 12, 2014 at 21:07
Johansrk
5,2803 gold badges40 silver badges39 bronze badges
4 Answers 4
var names = "Verwerkende industrie, Retail, Primaire producent, Out of home, Groothandel, Grondstof, Consument, Bewerkende industrie";
var numbers = "9, 3, 4, 2, 7, 9, 3, 2";
var namesArray = names.split(","); //split the string at ','. split() returns an array of result
var numbersArray = numbers.split(",");
var resultArray = []; //array to hold result
//since the namesArray and numbersArray are the same length, you can use one for-loop
for (var i=0, len=namesArray.length; i < len; i++) {
resultArray[i] = [namesArray[i], parseInt(numbersArray[i])];
}
answered Apr 12, 2014 at 21:17
Akinkunle Allen
1,30912 silver badges20 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Johansrk
Works perfectly. Thanks.. And even with line comments
This should work.
var names = "Verwerkende industrie, Retail, Primaire producent, Out of home, Groothandel, Grondstof, Consument, Bewerkende industrie";
var numbers = "9, 3, 4, 2, 7, 9, 3, 2";
names = names.split(',');
numbers = numbers.split(',');
var combined = [];
for(var i = 0; i < names.length; i++){
combined.push([names[i], parseInt(numbers[i])]);
}
answered Apr 12, 2014 at 21:17
Tyler McGinnis
35.4k16 gold badges75 silver badges79 bronze badges
Comments
Unfortunately JavaScript doesn't have a 'zip' function, but you can do the same thing like this:
var namesArr = names.split(", ");
var numbersArr = numbers.split(", ").map(function (s) { return parseInt(s); });
var combined = [];
for (var i = 0; i < namesArr.length; i++)
{
combined[i] = [namesArr[i], numbersArr[i]];
}
answered Apr 12, 2014 at 21:17
Kendall Frey
44.6k21 gold badges113 silver badges151 bronze badges
1 Comment
Johansrk
close but some of them result in a NaN
var names = "Verwerkende industrie, Retail, Primaire producent, Out of home, Groothandel, Grondstof, Consument, Bewerkende industrie";
var numbers = "9, 3, 4, 2, 7, 9, 3, 2";
function zip(arrays) {
return arrays[0].map(function(_,i){
return arrays.map(function(array){return array[i]})
});
}
// paramert = array
zip([
names.split(/,\s+/),
numbers.split(/,\s+/).map(function (i) { return parseInt(i); })
]);
answered Apr 12, 2014 at 21:10
niklasfi
16.1k7 gold badges44 silver badges55 bronze badges
3 Comments
Ingo Bürk
This does not give the desired array. For one the whitespace is not stripped, secondly the numbers will be strings (the example output states they are number types).
niklasfi
@IngoBürk sorry for the mix up. I was in a hurry
Johansrk
quite close. When I console.log it it gives me the structure, but all numbers are undefined in the array
lang-js
numbersis not valid Javascript syntax. Is it supposed to be an array? Or a string likenames? Also, are you sure you wouldn't rather have an array of objects than an array of arrays?