Javascript-从JSON对象中检索特定键值

时间:2016-07-14 06:42:42

标签: javascript json

我想从Json对象中仅检索某些键值对。在具有唯一ID的用户列表下方作为键。

var user =   {
    "987654321": {
      "displayName": "Wayne",
      "profilepic": "https://test1.com",
      "fullName": "Bruce Wayne",
      "Address": "1st Main 1st Cross",
      "dob": "Apr 1986",
      "likes": "Sports"
    },
    "123456789": {
      "displayName": "wade",
      "profilepic": "https://test2.com",
      "fullName": "wade wilson",
      "Address": "31 Main 1st Cross",
      "dob": "Sep 1993",
      "likes": "Movies"
    }
}
var final = {};
for (var key in user){
final[key] = {"displayName" : user[key]["displayName"]};
final[key] = {"fullName" : user[key]["fullName"]};
final[key] = {"profilepic" : user[key]["profilepic"]};
}
console.log(final);

以下是我得到的输出

{
    "715886684760616961": {
      "profilepic": "https://test1.com"
    },
    "716503112127758336": {
      "profilepic": "https://test2.com"
    }
  }

我想获取displayName,fullName&用户照片。但我最终用profilepic覆盖它。请告诉我如何解决这个问题。

谢谢

4 个答案:

答案 0 :(得分:3)

var user =   {
    "987654321": {
      "displayName": "Wayne",
      "profilepic": "https://test1.com",
      "fullName": "Bruce Wayne",
      "Address": "1st Main 1st Cross",
      "dob": "Apr 1986",
      "likes": "Sports"
    },
    "123456789": {
      "displayName": "wade",
      "profilepic": "https://test2.com",
      "fullName": "wade wilson",
      "Address": "31 Main 1st Cross",
      "dob": "Sep 1993",
      "likes": "Movies"
    }
}
var final = {};
for (var key in user){
final[key] = {"displayName" : user[key]["displayName"],
"fullName" : user[key]["fullName"],
"profilepic" : user[key]["profilepic"]};
}
console.log(final);

请参阅附件代码.. 你覆盖价值很简单..所以只需追加

  

final [key] = {“displayName”:user [key] [“displayName”],“fullName”:   user [key] [“fullName”],“profilepic”:user [key] [“profilepic”]};

答案 1 :(得分:1)

您将获得唯一的最后一个密钥对,因为在删除前一个密钥的行范围后将被删除。

<script>
var final = {};
for (var key in user){
final[key] = {"displayName" : user[key]["displayName"],"fullName" : user[key]["fullName"],"profilepic" : user[key]["profilepic"]}

} 
</script>

答案 2 :(得分:1)

您在每一行都覆盖final[key],只获得最后一次分配。

您可以创建一个新对象,如果没有,并首先分配一个对象

final[key] = final[key] || {};

然后将属性分配给它。

final[key].displayName = user[key]["displayName"];
final[key].fullName = user[key]["fullName"];
final[key].profilepic = user[key]["profilepic"];

&#13;
&#13;
var user = { "987654321": { "displayName": "Wayne", "profilepic": "https://test1.com", "fullName": "Bruce Wayne", "Address": "1st Main 1st Cross", "dob": "Apr 1986", "likes": "Sports" }, "123456789": { "displayName": "wade", "profilepic": "https://test2.com", "fullName": "wade wilson", "Address": "31 Main 1st Cross", "dob": "Sep 1993", "likes": "Movies" } },
    final = {};

for (var key in user) {
    final[key] = final[key] || {};
    final[key].displayName = user[key]["displayName"];
    final[key].fullName = user[key]["fullName"];
    final[key].profilepic = user[key]["profilepic"];
}

console.log(final);
&#13;
&#13;
&#13;

答案 3 :(得分:1)

原因是你有三次重新分配一个新对象(只要你使用{...}就会创建它。

你可能想要的是这样的:

for (var key in user) {
    // Create a new object only once
    var newEntry = {};

    // Store a reference to the original entry
    var oldEntry = user[key]

    // Copy members
    newEntry.displayName = oldEntry.displayName;
    newEntry.fullName = oldEntry.fullName;
    newEntry.profilepic = oldEntry.profilepic;

    // Assign the object
    final[key] = newEntry;
}

您可以使用括号和字符串索引,而不是使用.运算符,但我认为使用.更容易阅读。

请注意,您还可以存储要复制到其他数组中的字段,这对于更具动态性/选择性非常有用:

// Members to copy
var copyStuff = ['displayName', 'fullName', 'profilePic'];

for (var key in user) {
    // Create a new object only once
    var newEntry = {};

    // Store a reference to the original entry
    var oldEntry = user[key]

    // copy members
    for (var subkey in copyStuff)
        newEntry[subkey]= oldEntry[subkey];

    // Assign the object
    final[key] = newEntry;
}