当前位置: 首页 > 面试题库 >

比较两个json数组

卫高明
2023-03-14
问题内容

我有这个间隔,当前每5秒执行一次ajax请求。我对if声明有疑问。我的代码总是输入它,并且两个json值完全相同,为什么它认为它们不同?

var newActivity = null, oldActivity = null;
setInterval(function(){
    $.ajax({
        type: "get",
        url: "/get/new_activity",
        dataType: "json",
        success: function(data){
            oldActivity = newActivity;
            newActivity = data;
            console.log(JSON.stringify(oldActivity));
            console.log(JSON.stringify(newActivity));
            if(JSON.stringify(oldActivity) != JSON.stringify(newActivity)){
                $("#new-activity").slideDown( "fast" );
            }
        }
    });
}, 5000);

编辑
这是控制台输出(虚线是分隔请求,它不在实际输出中)

null
[{"title":"How many planets are in the solar system?","title_url":"How-many-planets-are-in-the-solar-system%3F","id":"2","answers":"1","asked":"2013-01-11 10:03:50","asked_pretty":"Today","activity":"2013-01-11 12:33:53","activity_pretty":"Today"},{"title":"Why is the sky blue?","title_url":"Why-is-the-sky-blue%3F","id":"1","answers":"1","asked":"2013-01-11 09:55:13","asked_pretty":"Today","activity":"2013-01-11 12:03:45","activity_pretty":"Today"}]

---------------------------------------------------

[{"title":"How many planets are in the solar system?","title_url":"How-many-planets-are-in-the-solar-system%3F","id":"2","answers":"1","asked":"2013-01-11 10:03:50","asked_pretty":"Today","activity":"2013-01-11 12:33:53","activity_pretty":"Today"},{"title":"Why is the sky blue?","title_url":"Why-is-the-sky-blue%3F","id":"1","answers":"1","asked":"2013-01-11 09:55:13","asked_pretty":"Today","activity":"2013-01-11 12:03:45","activity_pretty":"Today"}]
[{"title":"How many planets are in the solar system?","title_url":"How-many-planets-are-in-the-solar-system%3F","id":"2","answers":"1","asked":"2013-01-11 10:03:50","asked_pretty":"Today","activity":"2013-01-11 12:33:53","activity_pretty":"Today"},{"title":"Why is the sky blue?","title_url":"Why-is-the-sky-blue%3F","id":"1","answers":"1","asked":"2013-01-11 09:55:13","asked_pretty":"Today","activity":"2013-01-11 12:03:45","activity_pretty":"Today"}]

---------------------------------------------------

[{"title":"How many planets are in the solar system?","title_url":"How-many-planets-are-in-the-solar-system%3F","id":"2","answers":"1","asked":"2013-01-11 10:03:50","asked_pretty":"Today","activity":"2013-01-11 12:33:53","activity_pretty":"Today"},{"title":"Why is the sky blue?","title_url":"Why-is-the-sky-blue%3F","id":"1","answers":"1","asked":"2013-01-11 09:55:13","asked_pretty":"Today","activity":"2013-01-11 12:03:45","activity_pretty":"Today"}]
[{"title":"How many planets are in the solar system?","title_url":"How-many-planets-are-in-the-solar-system%3F","id":"2","answers":"1","asked":"2013-01-11 10:03:50","asked_pretty":"Today","activity":"2013-01-11 12:33:53","activity_pretty":"Today"},{"title":"Why is the sky blue?","title_url":"Why-is-the-sky-blue%3F","id":"1","answers":"1","asked":"2013-01-11 09:55:13","asked_pretty":"Today","activity":"2013-01-11 12:03:45","activity_pretty":"Today"}]

问题答案:

不能保证以相同的方式序列化JSON对象,也不能保证属性以相同的顺序进行序列化,使用JSON.stringify并不是测试对象相等性的好方法。

一个更好的例子是这样的函数(前一段时间在互联网上找到,希望我能感谢原始作者)

/**
 * Deep compare of two objects.
 *
 * Note that this does not detect cyclical objects as it should.
 * Need to implement that when this is used in a more general case. It's currently only used
 * in a place that guarantees no cyclical structures.
 *
 * @param {*} x
 * @param {*} y
 * @return {Boolean} Whether the two objects are equivalent, that is,
 *         every property in x is equal to every property in y recursively. Primitives
 *         must be strictly equal, that is "1" and 1, null an undefined and similar objects
 *         are considered different
 */
function equals ( x, y ) {
    // If both x and y are null or undefined and exactly the same
    if ( x === y ) {
        return true;
    }

    // If they are not strictly equal, they both need to be Objects
    if ( ! ( x instanceof Object ) || ! ( y instanceof Object ) ) {
        return false;
    }

    // They must have the exact same prototype chain, the closest we can do is
    // test the constructor.
    if ( x.constructor !== y.constructor ) {
        return false;
    }

    for ( var p in x ) {
        // Inherited properties were tested using x.constructor === y.constructor
        if ( x.hasOwnProperty( p ) ) {
            // Allows comparing x[ p ] and y[ p ] when set to undefined
            if ( ! y.hasOwnProperty( p ) ) {
                return false;
            }

            // If they have the same strict value or identity then they are equal
            if ( x[ p ] === y[ p ] ) {
                continue;
            }

            // Numbers, Strings, Functions, Booleans must be strictly equal
            if ( typeof( x[ p ] ) !== "object" ) {
                return false;
            }

            // Objects and Arrays must be tested recursively
            if ( !equals( x[ p ],  y[ p ] ) ) {
                return false;
            }
        }
    }

    for ( p in y ) {
        // allows x[ p ] to be set to undefined
        if ( y.hasOwnProperty( p ) && ! x.hasOwnProperty( p ) ) {
            return false;
        }
    }
    return true;
},


 类似资料:
  • 使用RestAssured,我试图比较两个JSON对象。 示例:第一个JSON来自excel,我将其作为字符串读取并存储 第二个 JSON 是返回响应对象的响应,该响应对象实际上是一个 JSON 我想将这两者作为JSON对象进行比较,因为当我将响应转换为String并尝试进行比较时,如果JSON模式的顺序发生变化,我的断言就会失败。 我尝试了将字符串转换成JSON的方法,但是没有找到任何方法。有人

  • 我有一个简单的javascript问题,我真的需要一些帮助!我正在尝试弄清楚如何在数组之间比较元素,以及在下一个循环中再次比较较大的元素。假设我们有数组A和数组B。 我的问题是,在比较索引处的元素之后,我希望在下一个循环周期中比较较大的元素。 如果A=[5,7,4],B=[2,8,5] 在第一个循环中,5与2进行比较,2较小,因此会发生一些事情。在下一个循环周期中,我希望5与8进行比较,而对于现在

  • 问题内容: 短篇小说:如何比较两个JSON块?下面的代码错误。 长话短说:我正在做一些E2E测试,其中一部分我需要将请求的JSON正文与接收到的JSON进行比较。为此,我尝试将期望的接收到的json解组到一个空接口(以避免任何类型错误),但出现错误: 。我猜编码/ json不喜欢空接口,所以问题是如何比较两个JSON块?字符串比较容易出错,因此我尝试避免这种情况。 问题答案: 您需要将指针传递给和

  • 我怎么能那么做?

  • 问题内容: 我有一个带有〜已知二进制序列的字节数组。我需要确认二进制序列是应该的。除之外,我还尝试了其他方法,但均无济于事。 问题答案: 在您的示例中,您具有: 在处理对象时,java中会比较 参考值 。您正在检查对by返回的数组的引用是否与所保存的引用相同,这当然永远不会正确。此外,数组类不会覆盖,因此其行为仅是比较参考值。 为了比较两个数组的 内容 ,Arrays类提供了静态数组比较方法。

  • 过滤出数组中比较函数不返回 true 的所有值。 类似于difference ,除了接受一个 comparator (比较函数)。 使用 Array.filter() 和 Array.findIndex() 来查找合适的值。 const differenceWith = (arr, val, comp) => arr.filter(a => val.findIndex(b => comp(a, b