简体   繁体   English

如何检查JSON数组是否相等

[英]How to check if JSON array is equal to

I am creating pie charts with JSON and Flot. 我正在使用JSON和Flot创建饼图。 The JS function to create the pie chart receives a JSON array from Django in this format: 创建饼图的JS函数以这种格式从Django接收JSON数组:

[1, 3, 2, 5, 4]

If there is no data, the JSON array is: 如果没有数据,则JSON数组为:

[0, 0, 0, 0, 0]

I'm trying to adjust the function so that if there is no data, then the pie will not be plotted and some text will appear instead (eg "Nothing to show yet"). 我正在尝试调整函数,这样如果没有数据,则不会绘制饼图,而是会出现一些文本(例如“Nothing to yet yet”)。 So far I have tried: 到目前为止,我尝试过:

function loadWeekChart(theData) {
    var blankData = [0, 0, 0, 0, 0];
    if ($.data(theData) == $.data(blankData)){
    $('#week-pie-chart').empty().append('Nothing to show yet');
    } else {
        $.plot($("#week-pie-chart"), theData ,
            {
                series: {
                    pie: { 
                        show: true
                    }
                }
            });
     }
}

The JS doesn't fail, but it neither prints a pie chart (there is no data) nor does it give me the text replacement. JS不会失败,但它既不会打印饼图(没有数据),也不会给我文本替换。

Please can someone show me where I'm going wrong! 请有人告诉我我哪里出错了!

Personaly i would do the following heres the psuedo code... Personaly我会做以下继承人的伪代码...

set boolean to true
for each element in the JSON
    compare it with blank data element 
    if they are not equal boolean false
    else continue
return boolean

Then you will know if there same as that function returns true if they are false if they aren't. 然后,如果它们不是,那么你将知道如果它们是假的,那么该函数是否返回true。

Please let me know if you need help coding this. 如果您需要帮助编码,请告诉我。 Shouldn't that hard 不应该那么难

This may also help: Similar Question 这也可能有所帮助: 类似的问题

function checkJsons(otherJson,newJson)
{
    var sameJson = true;
     for (var key in otherJson) {
        if(otherJson[key] != newJson[key]) {sameJson=false;} return sameJson;
     }
}

That should help, not test though 这应该有所帮助,但不测试

A nicer way to do this but harder to read is 一个更好的方法,但更难以阅读

function checkJsons(otherJson,newJson)
{
  for (var key in otherJson) {if(otherJson[key] != newJson[key]) {return false;}}
  return true;
}

function pieChartData(theData)
{
  var blankData = [0, 0, 0, 0, 0];
 if(checkJsons(blankData,theData)){$('#week-pie-chart').empty().append('Nothing to show yet');} else { // do your code here // }
} 

I should think something like this should work: 我认为这样的事情应该有效:

var replace = true;
for(var i = 0; i < theData.length; i++)
{
    if(theData.[i] != 0)
    {
        replace = false;
        break;
    }
}

if(replace)
{
     $('#week-pie-chart').empty().append('Nothing to show yet');
}

else
{
    $.plot($("#week-pie-chart"), theData ,
        {
            series: {
                pie: { 
                    show: true
                }
            }
        });
}

how about use JSON.stringify? 如何使用JSON.stringify?

l1 = [0, 0, 0, 0];
l2 = [0, 0, 0, 1];
var bEqual = JSON.stringify(l1) == JSON.stringify(l2);
console.log(bEqual);

You don't need to compare with 0-filled array. 您不需要与0填充数组进行比较。 Just check the input array is 0-filled. 只需检查输入数组是否为0。 I think reduce() is the best for this: 我认为reduce()是最好的:

if(arr.reduce(function(a, b){ return a + b; }) == 0) {
    //Nothing to show
} else {
    ...

...but older browsers don't support reduce(), so I suggest using $.grep() instead (because you're already using jQuery). ...但旧的浏览器不支持reduce(),所以我建议使用$.grep()代替(因为你已经在使用jQuery)。

if($.grep(theData, function (a) { return a != 0; }).length == 0) {
    //Nothing to show
} else {
    ...

LmC's answer is good for comparing to JSON objects. LmC的答案很适合与JSON对象进行比较。 I adapted it to compare two JSON arrays which can even have nested JSON arrays. 我改编它来比较两个甚至可以嵌套JSON数组的JSON数组。 Here is the code: 这是代码:

var areJSONArraysEqual = function(jsonArray1,
                                    jsonArray2) {

                                if(jsonArray1.length===0||jsonArray2.length===0){
                                    if(jsonArray1.length===0 && jsonArray2.length===0){
                                        return true;
                                    }else{
                                        return false;
                                    }
                                }

                                for(var i=0;i<jsonArray1.length;i++){
                                    for ( var key in jsonArray1[i]) {
                                        if(jsonArray1[i][key].length>1){
                                            return areJSONArraysEqual(jsonArray1[i][key],jsonArray2[i][key])
                                        }
                                        if (jsonArray1[i][key] != jsonArray2[i][key]) {
                                            return false;
                                        }
                                    }
                                }
                                return true;
                            };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM