0

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

2
  • how you detect order properties in object, i mean why male map on 1 and not 7th? Commented Nov 19, 2015 at 18:22
  • 1
    Your target output seems weird, shouldn't it be an object... Commented Nov 19, 2015 at 18:32

2 Answers 2

0

Properties of objects have no order. But if the order in not important, the I propose this solution:

var array = [1, 2, 3, 4, 2],
    object = {
        gender: ['male', 'female'],
        grade: ['7th', '8th', '9th'],
    },
    newObject = {},
    i = 0;

Object.keys(object).forEach(function (a) {
    newObject[a] = newObject[a] || {};
    object[a].forEach(function (b) {
        newObject[a][b] = array[i];
        i++;
    });
});

document.write('<pre>' + JSON.stringify(newObject, 0, 4) + '</pre>');

For an order proof object, I suggest to use arrays in combination with objects.

var array = [1, 2, 3, 4, 2],
    object = [
        { gender: ['male', 'female'] },
        { grade: ['7th', '8th', '9th'] }
    ],
    newObject = {},
    i = 0;

object.forEach(function (a) {
    Object.keys(a).forEach(function (b) {
        newObject[b] = newObject[b] || {};
        a[b].forEach(function (c) {
            newObject[b][c] = array[i];
            i++;
        });
    });
});

document.write('<pre>' + JSON.stringify(newObject, 0, 4) + '</pre>');

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

2 Comments

Order is important, thank you so very much. Any chance you would comment on your code? Specifically, what's the newObject[b] || {}
@icicleking, it is an check if newObject[b] is falsey (undefined) and takes a logic or with an object. the assign value is either the object or if not set an empty object.
0

The following Snippet creates the target object, and it will work in most browsers.

However, note that object keys are not guaranteed to be ordered. So the output could potentially be this:

targetObject = {
  grade: [
    7th: 1,
    8th: 2,
    9th: 3,
  ],
  gender: [
    male: 4,
    female: 2,
  ]
}

Snippet:

var array = [1, 2, 3, 4, 2],
    object = {
      gender: ['male', 'female'],
      grade:  ['7th', '8th', '9th']
    },
    targetObject= {},
    i,
    j,
    k= 0;

for(var i in object) {
  targetObject[i]= targetObject[i] || {};   //initialize if needed
  object[i].forEach(function(key) {         //iterate through the keys
    targetObject[i][key]= array[k++];       //assign to the next array element
  });
}

document.querySelector('pre').textContent= JSON.stringify(targetObject, 0, 2);  //show targetObject
<pre></pre>

Comments

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.