Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

javascript - search in JSON with AJAX

I have to search inside a json file with a value from my input, everything is fine capturing the value and the event, but when I iterate something unexpected happens for me. How can I select an object based on this search?. the problem is that when doing .each it goes through all the records, even those not found.

$( document ).on('turbolinks:load', common_events)

function common_events(){
    $('.rut-input').on('change', function(event){
        $rut = $(this).val();
        var searchField = $rut;
        var expression = new RegExp(searchField, "i");
        event.preventDefault();
        $.ajax({
            type: "GET",
            url: '/companies/',
            dataType: 'JSON',
            success: function(companies){
                $.each(companies, function(i, company) {
                    if (company.rut.search(expression) > -1){
                        console.log(company.id);

                        $('.name-input').empty();
                        $('.name-input').val(company.name);
                        $('.name-input').addClass('disabled-input');
                        $('.form-group').removeClass('hide');
                        console.log(company.address);
                        if (company.address == null ){
                            $('.address-input').removeClass('disabled-input');
                        };
                    }
                    else {
                        console.log('no encuentra');
                        console.log(company);
                        $('.form-group').removeClass('hide');
                        $('.form-control').removeClass('disabled-input');
                    };
                });
            },
            error: function(companies){
                console.log('A ocurrido un error')
            },
        });
    });
}

when executing the event, both the if and else are executed at the same time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Seems like you don't really want to iterate the companies array with each. You want to find a matching company, and then do something with that record.

success: function(companies){
    const company = companies.find(c => c.rut.search(expression) > -1);
    
    if (company) {
        console.log(company.id);

        $('.name-input').empty();
        $('.name-input').val(company.name);
        $('.name-input').addClass('disabled-input');
        $('.form-group').removeClass('hide');
        console.log(company.address);
        if (company.address == null ){
            $('.address-input').removeClass('disabled-input');
        }
    } else {
        console.log('no encuentra');
        console.log(company);
        $('.form-group').removeClass('hide');
        $('.form-control').removeClass('disabled-input');
    }
},

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...