0

I tried several approches and none of them works. I think this is because I am using JSON returned by django DRF. I want to create a list of IFSC using this JSON in Jquery in my HTML template itself. This is how my api returns JSON for any queryset.

{
  "count": 134,
  "next": "http://127.0.0.1:8000/api/bankdetailapi/?limit=5&offset=5&q=ABHY",
  "previous": null,
  "results": [
    {
      "ifsc": "ABHY0065001",
      "bank": {
        "name": "ABHYUDAYA COOPERATIVE BANK LIMITED",
        "id": 60
      },
      "branch": "RTGS-HO",
      "address": "ABHYUDAYA BANK BLDG., B.NO.71, NEHRU NAGAR, KURLA (E), MUMBAI-400024",
      "city": "MUMBAI",
      "district": "GREATER MUMBAI",
      "state": "MAHARASHTRA"
    },
    {
      "ifsc": "ABHY0065002",
      "bank": {
        "name": "ABHYUDAYA COOPERATIVE BANK LIMITED",
        "id": 60
      },
      "branch": "ABHYUDAYA NAGAR",
      "address": "ABHYUDAYA EDUCATION SOCIETY, OPP. BLDG. NO. 18, ABHYUDAYA NAGAR, KALACHOWKY, MUMBAI - 400033",
      "city": "MUMBAI",
      "district": "GREATER MUMBAI",
      "state": "MAHARASHTRA"
    },
    {
      "ifsc": "ABHY0065003",
      "bank": {
        "name": "ABHYUDAYA COOPERATIVE BANK LIMITED",
        "id": 60
      },
      "branch": "BAIL BAZAR",
      "address": "KMSPM'S SCHOOL, WADIA ESTATE, BAIL BAZAR-KURLA(W), MUMBAI-400070",
      "city": "MUMBAI",
      "district": "GREATER MUMBAI",
      "state": "MAHARASHTRA"
    }
  ]
}

The code I tried:

$(document).ready(function(){ 
            var value = $('#q').val()
            $.getJSON("http://127.0.0.1:8000/api/bankdetailapi/?q="+ value, function(data){
                var text = `IFSC: ${data.ifsc}`
                })
)}

It throws error in browser console that Uncaught ReferenceError: text is not defined . I want to use this IFSC list as autocomplete suggestions.

Jay Sardar
  • 19
  • 7

1 Answers1

0
       $("#q").keyup(function(){
        var value = $('#q').val()
        $.getJSON("http://127.0.0.1:8000/api/bankdetailapi/?q="+ value, function(data){
        var text = ['']
        for (var i=0;i<data.results.length;++i){text.push(data.results[i].ifsc)}
        // var text = `IFSC: ${data.results.ifsc}`

        console.log(text)

By using push method and iterating on whole data i managed to get list of all IFSC values.

Jay Sardar
  • 19
  • 7
  • Always better to code it yourself than be spoon fed. Can be just `var text=[];` to initialise the array. You can also use `var text = data.results.map(x=>x.ifsc)` for some shorthand. – freedomn-m Jun 28 '21 at 16:43
  • i am not comfortable with map. but i guess i have to start working on it. thanks again @freedomn-m – Jay Sardar Jun 28 '21 at 17:23
  • That's one of the reasons I suggested the `for` + `push` loop (first). `map` is shorthand for your `for` loop and array generation. It allows you to convert (or "map") from one format (an array with multiple properties) to an other format (an array with a single property) nice and easily. Build up your skills bit by bit and understand them as you go, rather than dive in and get confused :) – freedomn-m Jun 29 '21 at 15:37