请帮我打印控制台的值和“昵称”字段的键。这是JSON中的序列化数据数组:
[
{"fields": {"user_permissions": [], "nickname": "\u0418\u0432\u0430\u043d \u0422\u0443\u0440\u0433\u0435\u043d\u0435\u0432 \u0451\u043f\u0442\u0430", "groups": [], "avatar": "", "gender": 1, "skype": "dfsdf", "other": "zzzz", "phone": "2014-0921"}, "model": "app_accounts.userprofile_deferred_avatar_date_joined_email_fic679ed35161c7ba80f007b4235db5148", "pk": 4},
{"fields": {"user_permissions": [], "nickname": "\u0410\u043d\u0442\u043e\u043d \u0427\u0435\u0445", "groups": [], "avatar": "", "gender": 2, "skype": "dfsdf", "other": "qqqqqq", "phone": "21"}, "model": "app_accounts.userprofile_deferred_avatar_date_joined_email_fic679ed35161c7ba80f007b4235db5148", "pk": 5}
]
我希望结果是这样的:
0: zzzzzz
1: qqqqqq
我该怎么做?
答案 0 :(得分:1)
您在问题中提供的数组是not serialized。此外username
给定数组中的任何对象都没有字段。
如果您指的是每个other
对象中的字段fields
,则可以使用Array.map
检索该字段:
var usernames = [
{"fields": {"user_permissions": [], "nickname": "\u0418\u0432\u0430\u043d \u0422\u0443\u0440\u0433\u0435\u043d\u0435\u0432 \u0451\u043f\u0442\u0430", "groups": [], "avatar": "", "gender": 1, "skype": "dfsdf", "other": "zzzz", "phone": "2014-0921"}, "model": "app_accounts.userprofile_deferred_avatar_date_joined_email_fic679ed35161c7ba80f007b4235db5148", "pk": 4},
{"fields": {"user_permissions": [], "nickname": "\u0410\u043d\u0442\u043e\u043d \u0427\u0435\u0445", "groups": [], "avatar": "", "gender": 2, "skype": "dfsdf", "other": "qqqqqq", "phone": "21"}, "model": "app_accounts.userprofile_deferred_avatar_date_joined_email_fic679ed35161c7ba80f007b4235db5148", "pk": 5}
].map(function (v) { return v.fields.other });
//=> usernames now: ["zzzz", "qqqqqq"]
See also ...
答案 1 :(得分:0)
如果要打印索引和昵称,请考虑使用forEach:
var users = [
{"fields": {"user_permissions": [], "nickname": "\u0418\u0432\u0430\u043d \u0422\u0443\u0440\u0433\u0435\u043d\u0435\u0432 \u0451\u043f\u0442\u0430", "groups": [], "avatar": "", "gender": 1, "skype": "dfsdf", "other": "zzzz", "phone": "2014-0921"}, "model": "app_accounts.userprofile_deferred_avatar_date_joined_email_fic679ed35161c7ba80f007b4235db5148", "pk": 4},
{"fields": {"user_permissions": [], "nickname": "\u0410\u043d\u0442\u043e\u043d \u0427\u0435\u0445", "groups": [], "avatar": "", "gender": 2, "skype": "dfsdf", "other": "qqqqqq", "phone": "21"}, "model": "app_accounts.userprofile_deferred_avatar_date_joined_email_fic679ed35161c7ba80f007b4235db5148", "pk": 5}
]
users.forEach(function(users, index) {console.log(index + ': ' + users.fields.nickname);});
将打印:
0: Иван Тургенев ёпта
1: Антон Чех
你有没有期待?