Alex
Alex

Reputation: 59

How to determine if a record with a specific value exists in an array from a MSSQL api call using javascript

apologies if this is crazy simple or has been answered a million times before. I just don't seem to be able to make any sense of the other Q&A threads I've been reading.

I am trying to work out how to determine if an array has a particular value in one of it's fields accross all of the rows. In other words, I just want it to ask if there is a "preQualTypeID" of 1 in any of the rows, and if not, I'm getting it to hide a part of the modal.

here is the HTML code for the modal:

<div class="modal fade hide" id="preQualModal" tabindex="-1" role="dialog" aria-labelledby="preQualsModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title" id="preQualsModalLabel">
                    <i class="fas fa-graduation-cap"></i>
                    <span>Prerequisite Qualifications</span>
                </h3>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="col-12">
                    <div class="d-none" id="preQual_AccedemicDetails">
                        <div class="row pt-1 pb-3">
                            <div id="preQual_Academic">@*API data goes here*@</div>
                        </div>
                    </div>
                    <div class="d-none" id="preQual_AccessDetails">
                        <div class="row pt-1 pb-3">
                            <div id="preQual_Access">@*API data goes here*@</div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

And here is the Javascript that I'm trying to implement:

$(function () {
    $('#preQualModal').modal({
        keyboard: true,
        backdrop: "static",
        show: false
        
    }).on('show.bs.modal', function () {
        var url = "/api/PreQualViewData/";
        var qualId = $(event.target).closest('td').data('id');

        $.get(url + qualId, function (data) {
        
            var accedemicExist = data.some(function (data) { data.preQualTypeID === 1 });
            
            if (accedemicExist == true) {
                $('#preQual_AccedemicDetails').removeClass('d-none');
            };
            
        });
    });
});

The main thing that I can't get to work is this bit:

var accExist = data.some(function (data) { data.preQualTypeID === 1 });

It always results in "False" and I'm guessing it is an issue with my sytax. When I debug, it says that the "data.preQualTypeID" is undefined, so I'm guessing I've done this wrong, but I don't know where to start. I'm not experienced in Javascript and I find the language a bit confusing.

The API is being built by an ASP controller and looks like this (I've taken out some of the detail to make it easier to read):

[{"qualification":"Graduate Management Training Scheme","level":7,"preQualTypeID":2},
{"qualification":"Degree","level":6,"preQualTypeID":1},
{"qualification":"A Levels","level":3,"preQualTypeID":1},
{"qualification":"GCSEs","level":2,"preQualTypeID":1}]

If anyone can point me in the right direction, I'd be very greatful. Will update with the implementation and vote up those that help.

Thanks

Please let me know if I need to give anymore information.

Upvotes: 1

Views: 295

Answers (1)

Swati
Swati

Reputation: 28522

You can use filter to check if data which received from backend in ajax has preQualTypeID = 1 or not depending on this removeClass .

Demo Code :

var data = [{
    "qualification": "Graduate Management Training Scheme",
    "level": 7,
    "preQualTypeID": 2
  },
  {
    "qualification": "Degree",
    "level": 6,
    "preQualTypeID": 1
  },
  {
    "qualification": "A Levels",
    "level": 3,
    "preQualTypeID": 1
  },
  {
    "qualification": "GCSEs",
    "level": 2,
    "preQualTypeID": 1
  }
];
//filter json aray 
var accedemicExist = $(data)
  .filter(function(i, n) {
    return n.preQualTypeID === 1; //check if prequaltypeid=1
  });
//if length > 0 exist 
if (accedemicExist.length > 0) {
  console.log("it exist do something")
  //do something
} else {

  console.log("not there sorry")
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Upvotes: 1

Related Questions