Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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', 'doe@mail.com'],
newVals = ['Jo','Do','jo@mail.com'];

The new object should be as :

{
 "firstName": {
    "oldValue": "John",
    "newValue": "Jo"
 },
 "lastName": {
    "oldValue": "John",
    "newValue": "Do"
 },
 "email": {
    "oldValue": "doe@mail.com",
    "newValue": "jo@mail.com"
 }
}

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
215 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...