0

I have a json string which is resulted by an ajax call which looks like this

[ { 
"name":"sourabh", 
"userid":"soruabhbajaj", 
"id":"11", 
"has_profile_image":"0" }, 
{
"name":"sourabh", 
"userid":"sourabhbajaj", 
"id":"12", 
"has_profile_image":"0" 
}]

The page on my web app uses "id" value as the identification of an object. So I want to display the name of the user where ever users' name is required. What is the fastest way of getting the right user object to output the name or other values of user using the id value. Thanks in advance.

4
  • possible duplicate of Searching for an Object inside the JSON Commented Sep 23, 2011 at 14:42
  • 2
    Fishy... the question and every answer has received a downvote. Commented Sep 23, 2011 at 14:53
  • Please note that this question is about searching inside an array, while "Searching for an Object inside the JSON" is about searching inside an object. Commented Sep 23, 2011 at 14:58
  • I updated my answer to create a lookup table. Commented Sep 23, 2011 at 15:12

4 Answers 4

1

Ok... this answer provides for a lot of overhead at the start, but then makes it faster to reference by id later. It basically makes an array that has each item at the same index of its id. Like I said, a lot of overhead at first but little for later processing

var data = [{"name":"sourabh", 
"userid":"soruabhbajaj", 
"id":"11", 
"has_profile_image":"0" }, 
{"name":"sourabh", 
"userid":"sourabhbajaj", 
"id":"12", 
"has_profile_image":"0" }, 
{"name":"sourabh", 
"userid":"sourabhbajaj", 
"id":"27", 
"has_profile_image":"0" }, 
{"name":"sourabh", 
"userid":"sourabhbajaj", 
"id":"3", 
"has_profile_image":"0" }, 
{"name":"myname", 
"userid":"myuserid", 
"id":"5", 
"has_profile_image":"0" }, 
{"name":"sourabh", 
"userid":"sourabhbajaj", 
"id":"2", 
"has_profile_image":"0" }]

arr = [];

data.sort(function(a,b){return Number(a.id) - Number(b.id);})

var k = 0;
var offset = Number(data[0].id);
for(var i = offset; i <= Number(data[data.length - 1].id);i++)
  if(Number(data[k].id) === i)
    arr.push(data[k++]);
  else
    arr.push({})

//now you can reference like so:
alert(arr[5 - offset].name) // alerts "myname"
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, This can be the answer i was waiting for. I think I am satisfied with your answer. Thanks dude, A +1 + acceptance.. :)
Be cautious, depending on the range of the user ids this method could create an enormous array and a huge number of empty objects. For example, if user ids start at 5000, the array will be at least 5000 elements large and at least 5000 objects will be created.
@Greg good point. I've updated my answer to help that a little at least.
0

Make the result from ajax call like this:

{ "11": { 
"name":"sourabh", 
"userid":"soruabhbajaj", 
"id":"11", 
"has_profile_image":"0" }, 
"12": {
"name":"sourabh", 
"userid":"sourabhbajaj", 
"id":"12", 
"has_profile_image":"0" 
}}

this way you can use it from javascript like this:

userName = jsonResponse[id].name

1 Comment

I was thinking about this method but this increases overhead on my controller of formatting the data in this format.
0
var data = [{"name":"sourabh", 
"userid":"soruabhbajaj", 
"id":"11", 
"has_profile_image":"0" }, 
{"name":"sourabh", 
"userid":"sourabhbajaj", 
"id":"12", 
"has_profile_image":"0" }]

for(var i in data)
  if(data[i]["id"] == 11)
    alert(data[i].name);

It's very simple :)

2 Comments

Its simple but using a loop will slow down my app. I am looking for a more efficient way of doing this.
@Sourabh are the id's contiguous (1,2,3,4...12) without any breaks in between? (and are they in numerical order)
0

Either search the array each time

function getUser(users, id) {
  var user = null;
  $.each(users, function() {
    if (this.id === id) {
      user = this;
      return false; // break out of loop
    }
  });
  return user
}

or create a reusable lookup table (assumes unique user ids):

function toLookup(users) {
  var lookup = {};
  $.each(users, function() {
    lookup[this.id] = this;
  });
  return lookup;
}

function getUser(lookup, id) {
  return lookup[id] || null;
}

1 Comment

I had a couple bugs in the first version - fixed now.

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.