0

A little bit stuck on the following scenario. I have three arrays, and using the arrays would like to create a new object.

var fields = ['firstName', 'lastName', 'email'],
oldVals = ['John', 'Doe', '[email protected]'],
newVals = ['Jo','Do','[email protected]'];

The new object should be as :

{
 "firstName": {
    "oldValue": "John",
    "newValue": "Jo"
 },
 "lastName": {
    "oldValue": "John",
    "newValue": "Do"
 },
 "email": {
    "oldValue": "[email protected]",
    "newValue": "[email protected]"
 }
}

Thanks in advance.

6
  • Are you asking for a function that would convert these arrays into an object? Commented May 23, 2016 at 16:54
  • What part of this are you stuck on? What have you done so far to solve this? Commented May 23, 2016 at 16:54
  • 4
    What happened to Doe? Commented May 23, 2016 at 16:55
  • What is the source of this array ? Depending on the indexes looks ugly to me... Commented May 23, 2016 at 16:56
  • @TamasHegedus: I'm guessing a typo :P Commented May 23, 2016 at 16:56

5 Answers 5

6
// first check that your arrays are actually all the same length. Then...

var obj = {};
for(var i=0;i<fields.length;i++) {
    obj[fields[i]] = {
        oldValue: oldVals[i],
        newValue: newVals[i]
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

Assuming lengths are same, Using reduce function of array

fields.reduce(function(res,x,index){
  res[x] = { 
   oldValue:oldVals[index],
   newValue:newVals[index]
   }
  return res;
},{});

Comments

1

A proposal with Array#forEach and some arrays for dynamic generation of the object.

var fields = ['firstName', 'lastName', 'email'],
    oldVals = ['John', 'Doe', '[email protected]'],
    newVals = ['Jo', 'Do', '[email protected]'],
    object = function (array, keys1, keys2) {
        var r = {};
        keys1.forEach(function (k1, i) {
            r[k1] = r[k1] || {};
            keys2.forEach(function (k2, j) {
                r[k1][k2] = array[j][i];
            });
        });
        return r;
    }([oldVals, newVals], fields, ['oldVals', 'newVals']);

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

Comments

0

A property can be created dynamically thus:

objectName[propertyVariable] = valueVariable;

So in the present case something like this would do the trick.

var object1 = {};
object1[fields[0]] = { oldValue: oldVals[0], newValue: newVals[0] } 

Comments

0

I wanted to add this solution , which will encapsulate it in a method, like this :

var fields = ['firstName', 'lastName', 'email'],
oldVals = ['John', 'Doe', '[email protected]'],
newVals = ['Jo','Do','[email protected]'];


function createCustomObj(fields , oldVals, newVals){
var obj={};

    fields.forEach( function(f, i){
        obj[f]={};
    obj[f]['oldValue']= oldVals[i];
    obj[f]['newValue']= newVals[i];
});

return obj;

};

console.log(createCustomObj(fields, oldVals, newVals));

https://jsfiddle.net/x54b0rhk/

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.