检查Json数组中是否存在特定密钥

时间:2021-01-02 23:46:27

I an trying to produce a check to determine if a specific key exists in a json array using jQuery. Specifically, if I see this key, I know that that no objects that I want to display on the page were returned from the server.

我试图生成一个检查,以确定使用jQuery在json数组中是否存在特定的键。具体来说,如果我看到这个密钥,我知道我没有想要在页面上显示的对象从服务器返回。

The simple check that I'm trying to use looks like this:

我正在尝试使用的简单检查如下所示:

 if (data.hasOwnProperty('Error - No records found in table')) {
  alert('true');
 }

The array that is returned from the server looks like this:

从服务器返回的数组如下所示:

[{"Error - No records found in table": ""}]

Full Code:

var url = 'https://blahblah.com';
var postData = $('#BranchSpecials').serialize();
var spinnerBig = $('.loadingSpinner');

var getClearanceItems = $.ajax({
 type: 'Post',
 url: url,
 xhrFields: {
  withCredentials: true
 },
 crossDomain: true,
 data: postData,
 dataType: 'json',
 beforeSend: function(xhr) {
  spinnerBig.show();
 }
});

getClearanceItems.done(function(data, jqXHR) {
 var clearance = $("#clearance");
 spinnerBig.hide();
 clearance.empty();

 if (data.hasOwnProperty('Error - No records found in table')) {
  alert('true');
 }

 var items = [];
 $.each(data, function(i, item) {
  items.push('<div class="item-block"><a href="' + url + item.ProdLink + '"><img src="https://blahblah.com/Data/' + item.ProdImage + '" width="104" height="104"/></a><div class="item-meta"><p class="desc"><a href="' + url + item.ProdLink + '">' + item.ProductDesc + '</a></p><p class="itemID">Item #: <a href="' + url + item.ProdLink + '" class="uline">' + item.ProductID + '</a></p></div></div>');
 });

 clearance.append(items.join(''));

 clearance.slick({
  infinite: true,
  slidesToShow: 4,
  slidesToScroll: 4,
  dots: true,
  appendArrows: $('#controls'),
  prevArrow: '<button type="button" class="btn btn-default"><i class="material-icons">chevron_left</i></button>',
  nextArrow: '<button type="button" class="btn btn-default"><i class="material-icons">chevron_right</i></button>'
 });
});

Question: What is wrong with this check?

问题:这张支票有什么问题?

2 个解决方案

#1


1  

You are checking the property defined for the array not the object, if the array contains single object then get the first object from array and check

您正在检查为数组定义的属性而不是对象,如果数组包含单个对象,则从数组中获取第一个对象并检查

if(data[0].hasOwnProperty('Error - No records found in table'))

if multiple elements are there then try the other answer using some() by @AmirPopovich.

如果有多个元素,那么使用@AmirPopovich的some()尝试另一个答案。

#2


0  

The problem is that data is an array and you want to perform the check on the array's items.

问题是数据是一个数组,您想要检查数组的项目。

Use Array.prototype.some in order to determine is any object in the array contains a key with the error.

使用Array.prototype.some以确定数组中的任何对象是否包含带错误的键。

var data = [{"Error - No records found in table": ""}];
if (data.some(function(item){ return item.hasOwnProperty('Error - No records found in table') })) {
  alert('true');
 }

#1


1  

You are checking the property defined for the array not the object, if the array contains single object then get the first object from array and check

您正在检查为数组定义的属性而不是对象,如果数组包含单个对象,则从数组中获取第一个对象并检查

if(data[0].hasOwnProperty('Error - No records found in table'))

if multiple elements are there then try the other answer using some() by @AmirPopovich.

如果有多个元素,那么使用@AmirPopovich的some()尝试另一个答案。

#2


0  

The problem is that data is an array and you want to perform the check on the array's items.

问题是数据是一个数组,您想要检查数组的项目。

Use Array.prototype.some in order to determine is any object in the array contains a key with the error.

使用Array.prototype.some以确定数组中的任何对象是否包含带错误的键。

var data = [{"Error - No records found in table": ""}];
if (data.some(function(item){ return item.hasOwnProperty('Error - No records found in table') })) {
  alert('true');
 }