Моя проблема довольно проста. Я думаю, я просто что-то отсутствует. Я в принципе хочу итерацию и добавить вещи к объекту, так что я мог бы в конечном итоге получить 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"
}]
}]
}
Я попытался сделать idm
, em
и fm
массивы затем выдвигая те, в мой объект, но я получаю сообщение об ошибке
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"
``
Любое руководство будет оценено.