user8334943
user8334943

Reputation: 1557

how to access key/value pairs from json() object?

I'm calling an external service and I get the returned domain object like this:

var domainObject = responseObject.json();

This converts the response object into a js object. I can then easily access a property on this object like this

var users = domainObject.Users

Users is a collection of key/value pairs like this:

1: "Bob Smith"
2: "Jane Doe"
3: "Bill Jones"

But CDT shows users as Object type and users[0] returns undefined. So how can I get a handle to the first item in the collection? I'm assuming that some type of type cast is needed but not sure how I should go about doing this

UPDATE

Here is one way I could access the values:

//get first user key
Object.keys(responseObject.json().Users)[0]

//get first user value
Object.values(responseObject.json().Users)[0]

But I need to databind through ng2 so I was hoping for a simpler way like this:

<div>
    <div *ngFor="let user of users">
        User Name: {{user.value}}
        <br>
    </div>
</div>

Maybe I should just create a conversion function in my ng2 component which converts the object into what I need before setting the databinding variable?

Upvotes: 0

Views: 1057

Answers (3)

joshrathke
joshrathke

Reputation: 7774

UPDATED ANSWER

So after scouring through a few docs I found the "newish" Object.entries() javascript function. You can read about it here. Pretty cool.

Anyways, give this a try. I am ashamed to say that I don't have time to test it, but it should get you going in the right direction.

usersArray = []

// Turn Users object into array of [key, value] sub arrays.
userPairs = Object.entries(users);

// Add the users back into an array in the original order.
for (i=0; i < userPairs; i++) {
    usersArray.push(_.find(userPairs, function(userPair) { return userPair[0] == i }))
}

ORIGINAL ANSWER

I would use either underscore.js or lodash to do this. Both are super helpful libraries in terms of dealing with data structures and keeping code to a minimum. I would personally use the _.values function in lodash. Read more about it here.. Then you could use users[0] to retrieve the first item.

The only caveat to this is that lodash doesn't guarantee the iteration sequence will be the same as it is when the object is passed in.

users = _.values(users);
console.log(users[0]);

Upvotes: 1

jsegarra
jsegarra

Reputation: 99

If your initial object is just a plain object, how do you know it is sorted. Property members are not sorted, ie: looping order is nor guaranteed. I´d extract the user names into an array and the sort that array by the second word. This should work (as long as surnames are the second word, and only single spaces are used as separators).

var l=[];
for(var x in users) {
 push.l(users[x]);
}
var l1=l.sort ( (a,b) => return a.split(" ")[1]<b.split(" ")[1]);

Upvotes: 0

DeborahK
DeborahK

Reputation: 60518

How about this:

let user= this.users.find(() => true)

This should return the "first" one.

Upvotes: 1

Related Questions