【问题标题】:How do I design an object in such a way I could formulate a JSON like this?我如何设计一个对象,以便我可以像这样制定一个 JSON?
【发布时间】:2019-06-27 13:44:34
【问题描述】:

我的问题很简单。我想我只是错过了一些东西。我基本上想迭代并将东西添加到一个对象中,以便我最终可以得到一个看起来像这样的 JSON:

{
    "user1": [{
        "idm": [{
            "name": "Jane Smith",
            "email": "user1@example.com"
        }],
        "em": [{
            "name": "Jane Smith",
            "email": "user1@example.com"
        }],
        "fm": [{
            "name": "Jane Smith",
            "email": "user1@example.com"
        }]
    }],
    "user2": [{
        "idm": [{
            "name": "John Smith",
            "email": "user2@example.com"
        }],
        "em": [{
            "name": "John Smith",
            "email": "user2@example.com"
        }],
        "fm": [{
            "name": "John Smith",
            "email": "user2@example.com"
        }]
    }]
}

我尝试创建 idmemfm 数组,然后将它们推送到我的对象中,但出现错误 ERROR TypeError: Unable to get property 'push' of undefined or null reference

我确信我缺少一些东西。

userData = {};
user = 'user1'

this.idm['name'] = name;
this.idm['email'] = email;
this.userData[user].push(this.idm);

this.em['name'] = name;
this.em['email'] = email;
this.userData[user].push(this.em);

this.fm['name'] = name;
this.fm['email'] = email;
this.userData[user].push(this.fm);

更新后:

this.userData = {};
array = ['user1', 'user2']
var i
for(i=0:i<array.length; ++i){
this.userData[array[i]] = []

    this.idm['name'] = name;
    this.idm['email'] = email;
    this.userData[user].push(this.idm);

    this.em['name'] = name;
    this.em['email'] = email;
    this.userData[user].push(this.em);

    this.fm['name'] = name;
    this.fm['email'] = email;
    this.userData[user].push(this.fm);
}

我在控制台中的输出类似于:

[object Object]
-user1
--0
---name "Jane Smith"
---emial "user1@example.com"
--1
---name "Jane Smith"
---emial "user1@example.com"
--2
---name "Jane Smith"
---emial "user1@example.com"
-user1
--0
---name "John Smith"
---emial "user2@example.com"
--1
---name John Smith"
---emial "user2@example.com"
--2
---name John Smith"
---emial "user2@example.com"
``

任何指导将不胜感激。

【问题讨论】:

    标签: javascript json angular data-structures


    【解决方案1】:

    你需要像这样声明数组:

    user = 'user1'
    this.userData = { [user]: [] }; // or this.userData = {}; this.userData[user] = []
    
    this.idm['name'] = name;
    this.idm['email'] = email;
    this.userData[user].push(this.idm);
    
    ...
    

    【讨论】:

    • 我的行为有点奇怪。所以我可以成功地为 idm 做 this.userData[user][0] 和为 em 做 this.userData[user][1] 但是当我尝试为 fm 做 this.userData[user][2] 时,我得到了未定义。我在调用 fm 数据到控制台之前打印了整个 userData,我看到它就在那里。知道为什么会这样吗?
    • 添加了一些东西
    • @newwebdev22 我看到你的更新,你写this.userData[user] 而不是this.userData[array[i]]
    • 嗯,我仍然遇到同样的问题。我只是不知道为什么我可以正确访问对象内 3 个数组中的 2 个而不是最后一个
    【解决方案2】:

    您尚未将 userData 定义为数组。

    userData=[]; idm=em=fm=[];
    idm.push({name:"Jane Smith", email:"user1@example.com"},{name:"Jane Smith", email:"user1@example.com"});
    em.push({name:"Jane Smith", email:"user1@example.com"},{name:"Jane Smith", email:"user1@example.com"});
    fm.push({name:"Jane Smith", email:"user1@example.com"},{name:"Jane Smith", email:"user1@example.com"});
    userData.push(idm);
    userData.push(em);
    userData.push(fm);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      相关资源
      最近更新 更多