2

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

Below is the output which i get

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

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.

Thank you

0

4 Answers 4

5

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much
2

You are getting the only last key pair because after terminating the line scope against previous key is removed.

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

} 
</script>

1 Comment

Thank you very much
1

You are overwriting final[key] in every line and get only the last assignment.

You could create a new object, if there is no one and assign first an object

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

and later assign the properties to it.

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

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

1 Comment

Thank you very much
1

The reason for this is you're three times reassigning a new object (which is created whenever you use {...}.

What you probably want is something like this:

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

Rather than using the . operator you could of course use brackets and string indices, but I think using . is easier to read.

Note that you could also store the fields to copy in an additional array, which could be useful to be more dynamic/selective:

// 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;
}

1 Comment

Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.