I have an array of values and an object where the values are smaller arrays:
array = [1, 2, 3, 4, 2]
object = {
gender: [male, female],
grade: [7th, 8th, 9th],
}
I want to zip the array and the object so that the values in the array are assigned to the new objects that are keyed with the values in the object, like this:
targetObject = {
gender: [
male: 1,
female: 2,
],
grade: [
7th: 3,
8th: 4,
9th: 2,
],
}
My first stab is to iterate through the object and create a new array
var newArray = [];
for(key in object) {
for(i=0;i<key.length;i++){
newArray.push(key[i]);
}
}
Then zip them together
var newObject = {};
for (var i = 0; i < newArray.length; i++) {
newObject[newArray[i]] = array[i];
}
If my syntax is write I believe I'm here:
array == [1, 2, 3, 4, 2]
object == {
gender: [male, female],
grade: [7th, 8th, 9th],
}
newArray == [male, female, 7th, 8th, 9th]
newObject == {
male: 1,
female: 2,
7th: 3,
8th: 4,
9th: 2,
}
It looks like I'm close, but I also feel like I'm stringing together a bunch of fragile code. Is there a better way? And if not, how do I go from my newObject to my targetObject
malemap on1and not7th?