如何检查是否JSON数组等于 [英] How to check if JSON array is equal to

查看:122
本文介绍了如何检查是否JSON数组等于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建饼图使用JSON和海军报。该JS函数来创建饼图接到此格式从Django的JSON数组:

I am creating pie charts with JSON and Flot. The JS function to create the pie chart receives a JSON array from Django in this format:

[1, 3, 2, 5, 4]

如果没有数据,JSON数组是:

If there is no data, the JSON array is:

[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 (e.g. "Nothing to show 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
                    }
                }
            });
     }
}

该JS不会失败,但它既不打印一个饼图(没有数据),也没有给我的文本替换。

The JS doesn't fail, but it neither prints a pie chart (there is no data) nor does it give me the text replacement.

请能有人告诉我我要去哪里错了!

Please can someone show me where I'm going wrong!

推荐答案

本人来说我会做以下继承人的伪code ...

Personaly i would do the following heres the psuedo code...

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

然后,你就会知道,如果他们是否是假的,如果他们不存在一样的函数返回true。

Then you will know if there same as that function returns true if they are false if they aren't.

请让我知道如果你需要帮助编码这一点。如若不是那么难

Please let me know if you need help coding this. Shouldn't that hard

这也可以帮助:类似的问题

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 // }
} 

这篇关于如何检查是否JSON数组等于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆