1
[Object, Object]
0:Object
 chat:"i like to say hello"
 id:13
 user_id:1
 yiinput_id:5
 __proto__:Object
1:Object
 chat:"another chat here"
 id:14
 user_id:1
 yiinput_id:5
 __proto__:Object
length:2
__proto__:Array[0]

I am trying to populate a list with some information from a table. I have the above object and I am tying to get values like this:

$.each(resp.chats, function() {
            $.each(this, function(k, v) {
                console.log(k + ' ' + v);//this gets me all keys and values but i need specific key value
            console.log("Chat ="+ //need chat value here );
            console.log("User ID ="+ //need id value here );
            });
        });

2 Answers 2

2

You can access the properties of the object within the chats array directly without needing another loop. Try this:

$.each(resp.chats, function(i, chat) {
    var value = chat.chat;
    var userId = chat.user_id;

    // use the variables as required here...
    console.log(value, userId);
});
Sign up to request clarification or add additional context in comments.

Comments

2

You can get it with structure.

$.each(resp.chats, function(index, element) {
    console.log("Chat ="+ element.chat );
    console.log("User ID ="+ element.user_id );
});

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.