0

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 e.g. 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"}} }

1 Answer 1

1

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; }

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

7 Comments

That works great, but how can I have results array to be in same format like myContacts in first example as now getting numbered/indexed one. Can perhaps write map() differently that wouldn't need to covert it?
@Aivaras did you mean, an approach using the function forEach?
No, just now results array is like this [{"john_doe_1":{"data":{""}}}, {"john_doe_2":{"data":{""}}}] so each user is indexed, need it to be like this instead [{ "john_doe_1":{"data":{""}}, "john_doe_2":{"data":{""}} }] and not sure how to.
@Aivaras but that is an array with only one object. Do you want that output?, or do you want this: { "john_doe_1": { "data": {"2": 1} }, "john_doe_2": { "data": {"a": 1} } }?
Yes like this, sorry shortened that for comment :) All same data that using (username,avatar_url) just need array in format like this.
|

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.