0

I have a json response like following

{"total":2,"data":[
            {"0":{"id":11,"name":"apple","image":"apple.png","description":"apple","status":1,"application_on":null,"created_at":"0000-00-00 00:00:00","updated_at":"1995-04-05 06:11:54",
        "fname":"apple","lname":"","profile_image":"<img src=\"\/pac\/public\/images\/brands\/60_60\/apple.png\"  \/>"},
            "1":{"id":16,"name":"chapstick","image":"chapstick.png","description":"chapstick","status":1,"application_on":null,"created_at":"0000-00-00 00:00:00","updated_at":"0000-00-00 00:00:00",
        "fname":"chapstick","lname":"","profile_image":"<img src=\"\/pac\/public\/images\/brands\/60_60\/chapstick.png\"  \/>"},"length":2}]}

How can i iterate this and take out fname from this ? Thanks in advance .

I use the following code for autocompletion

$(".textfield1").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "<?php echo $this->baseUrl('admin/ajax/global-search/') ?>",
                data: {q: request.term, },
                dataType: "json",
                success: function(data) {
                    response($.map(data, function(item) {
                        return {
                            value: item
                        };
                    }));
                }
            });
        }
    });

But it displaying "0",obj etcc..

2 Answers 2

1

Here is the body for your success() function:

var a = [];
for ( var i in data.data  ) {
    for ( var j in data.data[i]  ) {
        if ( 'undefined' !== typeof data.data[i][j].fname ) {
            a.push(data.data[i][j].fname);
        }
    }
}
response(a);
Sign up to request clarification or add additional context in comments.

Comments

0

Try to change to:

response($.map(data, function(item, index) {
    return {
        value: item[index.toString()]
    };
}));

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.