简体   繁体   中英

Javascript writing loop using value from json and passing it's nested data to loop as json object

I have a list of user uuids in myContactsUuids array, using forEach() method to loop through them and add user which is retrieved with new ChatEngine.User(uuid) function to myContacts array.

myContactsUuids = ["john_doe_001", "john_doe_005"];
// set a global array of users
myContacts = {};

myContactsUuids.forEach(function(uuid) {

 myContacts[uuid] = new ChatEngine.User(uuid);

});

Now trying to rewrite this to do essentially the same, but have additional data nested under each uuid and pass that as JSON object with user uuid as string in ChatEngine.User() function.

I have user data now like this, though can format in in any way.

myContactsUuids = {"john_doe_001":{"username":"John Doe","avatar_url":"http://someurl"},"john_doe_003":{"username":"Another John Doe","avatar_url":"http://someurl"}};

and ChatEngine.User(uuid,data) function where uuid is user uuid string and data json object so for eg user in loop look like this :

new $scope.ChatEngine.User("john_doe_001", {"username":"John Doe","avatar_url":"http://someurl"});

Just not sure what would be the best way to write loop for this and pass needed data to it, and then add retrieved user to array like did in simplified function. Perhaps I could do that using each() , but not sure how to correctly.

@Ele solution works, just need result array to be in format like this:

{ "john_doe_001":{"uuid":"john_doe_001","data":{"username":"John Doe","avatar_url":"http://someurl"}},"john_doe_003":{"uuid":"john_doe_003","data":{"username":"Another John Doe","avatar_url":"http://someurl"}} }

You can use the function Object.entries along with the function map

var result = Object.entries(myContactsUuids)
                   .map(([uuid, data]) => ({ [uuid]: new $scope.ChatEngine.User(uuid, data) }));

Example snippet:

 var ChatEngine = { User: function(uuid, data) { this.uuid = uuid; this.data = data; } } var myContactsUuids = {"john_doe_001":{"username":"John Doe","avatar_url":"http://someurl"},"john_doe_003":{"username":"Another John Doe","avatar_url":"http://someurl"}}; var result = Object.entries(myContactsUuids) .map(([uuid, data]) => ({ [uuid]: new ChatEngine.User(uuid, data) })); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Using the function reduce to build the desired output:

{
    "john_doe_1": {
        "data": {"2": 1}
        },
    "john_doe_2": {
        "data": {"a": 1}
    }
}

 var ChatEngine = { User: function(uuid, data) { this.uuid = uuid; this.data = data; } } var myContactsUuids = {"john_doe_001":{"username":"John Doe","avatar_url":"http://someurl"},"john_doe_003":{"username":"Another John Doe","avatar_url":"http://someurl"}}, result = Object.entries(myContactsUuids) .reduce((a, [uuid, data]) => { a[uuid] = new ChatEngine.User(uuid, data); return a; }, {}); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM