从JSON对象检索特定的键值

从JSON对象检索特定的键值

本文介绍了Javascript-从JSON对象检索特定的键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to retrieve only certain key value pair from Json object. Below the list of user with unique ids as keys.

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覆盖了它.请让我知道如何解决此问题.

I am trying to get displayName, fullName & profilepic. But i am ending up overwriting it with profilepic. Kindly let me know how to fix this.

谢谢

推荐答案

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

请参阅随附的代码.很简单,您正在覆盖值..因此只需在

Please refer the attached code..It's simple that you are overwriting values.. so simply append by

这篇关于Javascript-从JSON对象检索特定的键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:54