0

I have a function writen in ajax that return a set of value:

$.each(data.dataa, function (i,item) {
    $.each(item, function (index, dat) {
        $str+=dat;
    })

    $ulSub.append( '<li id="'+item.id +'" class="ui-widget-content">' +$str+'</li>');
});

Each item has two attribute: the id and the lastname, the value of each $str is a concatenation of the id and the lastname, but I want just the lastname not the id. I used the function item[2] but it's not working.

enter image description here

the result of my code is shwon as follow

enter image description here

What I want is just get the value of the lasname. I know that I should use item.lastname, but I want to ask if there are other methods to get the value of the lastname because the second attribute (lastname) is a variable.

13
  • 2
    In your $.each(item, check if index is the target variable's name, if it is, append the dat to the $str. Or var target = 'lastname'; //whatever it is., then $str += item[target]; Commented Nov 4, 2015 at 16:49
  • There's no "second" element in a JavaScript object. They are unordered. Can you show a sample object (not a picture of one) that includes two different "second" properties, and the HTML you'd like to see as a result? Your desired output is very unclear at the moment. Commented Nov 4, 2015 at 16:49
  • What do you mean with "because the second attribute (lastname) is a variable"? Commented Nov 4, 2015 at 16:51
  • I've edited my code and show the result of it, as you see there are 1, 3 ,4 , 6 but I want just the second element (lastname). in others case, the lastname can be changed by name , phone or any other attribute from database, so I cani't use the method of item.lastname Commented Nov 4, 2015 at 16:53
  • How is it decided which data to return? You must request the data somehow. Commented Nov 4, 2015 at 16:55

1 Answer 1

1

Well, your case is quite simple, you always have an object with 2 properties: id and an unknown property. As objects do not have a defined order, you can´t assume that the field you want is always in second position

One way is to iterate the keys, and pick the one that is not equal to id:

for(var key in item){ 
    if(key != 'id'){
        $str+= item[key]
    } 
}

A similar way is to pick the object keys, and filter out the id one, then access the object with that key:

 $str+= item[Object.keys(item).filter(function(k){return k != 'id'})[0]]

If you know the possible values of the key, another way would be:

var possibleKeys = ['lastname', 'name', 'adress', 'phonenumber']

 for(var key in item){ 
    if(possibleKeys.indexOf(key) != -1){ // if its a valid key, append the value
        $str+= item[key]
    } 
}   

In your case, the first option is probably the best, but there are mutiple ways to do it

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

Comments

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.