0

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]
            ];
2
  • 2
    The line for numbers is not valid Javascript syntax. Is it supposed to be an array? Or a string like names? Also, are you sure you wouldn't rather have an array of objects than an array of arrays? Commented Apr 12, 2014 at 21:08
  • ahh, sorry. It is also a string like names Commented Apr 12, 2014 at 21:10

4 Answers 4

1
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])];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly. Thanks.. And even with line comments
1

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

Comments

0

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]];
}

1 Comment

close but some of them result in a NaN
0
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); })
]);

3 Comments

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).
@IngoBürk sorry for the mix up. I was in a hurry
quite close. When I console.log it it gives me the structure, but all numbers are undefined in the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.