【问题标题】:Javascript writing loop using value from json and passing it's nested data to loop as json objectJavascript使用来自json的值编写循环并将其嵌套数据作为json对象传递给循环
【发布时间】:2018-03-10 23:03:10
【问题描述】:

我在 myContactsUuids 数组中有一个用户 uuid 列表,使用 forEach() 方法遍历它们并添加使用 new ChatEngine 检索的用户。 myContacts 数组的 User(uuid) 函数。

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

myContactsUuids.forEach(function(uuid) {

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

});

现在尝试重写它以执行基本相同的操作,但在每个 uuid 下嵌套了额外的数据,并将其作为 JSON 对象传递,用户 uuid 作为字符串在 ChatEngine.User() 函数中。 p>

我现在有这样的用户数据,但可以以任何方式格式化。

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

ChatEngine.User(uuid,data) 函数,其中 uuid 是用户 uuid 字符串和数据 json 对象,例如循环中的用户如下所示:

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

只是不确定为此编写循环并将所需数据传递给它的最佳方法是什么,然后像简化函数中那样将检索到的用户添加到数组中。也许我可以使用 each() 做到这一点,但不知道如何正确。

@Ele 解决方案有效,只需要结果数组的格式如下:

{ "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"}} }

【问题讨论】:

    标签: javascript arrays json


    【解决方案1】:

    您可以将函数 Object.entries 与函数 map 一起使用

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

    示例 sn-p:

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

    使用函数reduce 构建所需的输出:

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

    【讨论】:

    • 这很好用,但我怎样才能让结果数组的格式与第一个示例中的 myContacts 相同,因为现在正在编号/索引一个。也许可以用不同的方式编写 map() 而不需要隐藏它?
    • @Aivaras 你的意思是,使用函数forEach的方法?
    • 不,刚才的结果数组是这样的 [{"john_doe_1":{"data":{""}}}, {"john_doe_2":{"data":{""}} }] 所以每个用户都被索引,需要它是这样的 [{ "john_doe_1":{"data":{""}}, "john_doe_2":{"data":{""}} }] 而不是确定怎么做。
    • @Aivaras 但这是一个只有一个对象的数组。你想要那个输出?还是想要这个:{ "john_doe_1": { "data": {"2": 1} }, "john_doe_2": { "data": {"a": 1} } }
    • 是的,像这样,抱歉缩短了评论:) 使用 (username,avatar_url) 的所有相同数据只需要这样格式的数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 2021-07-30
    • 1970-01-01
    • 2018-07-23
    • 2019-08-21
    相关资源
    最近更新 更多