Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6066983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:33:33+00:00 2026-05-23T09:33:33+00:00

I have developed this method which compares 2 (or more) arrays, and returns whatever

  • 0

I have developed this method which compares 2 (or more) arrays, and returns whatever you wish as result.

This is done by converting the arrays to strings then comparing them, then converting back the result. (could also be used for other stuff) instead of just doing deep iterations and recursions to do the same thing..

example:

var arr1 = [[8,0,3,0,0,7,0,9,0],[0,9,0,0,3,0,0,0,0],[0,0,0,0,0,0,4,0,6],[0,0,0,0,3,9,7,6,0],[9,6,0,5,0,7,0,8,1],[0,7,4,6,8,0,0,0,0],[5,0,1,0,0,0,0,0,0],[0,0,0,0,5,0,0,7,0],[0,6,0,7,0,0,1,0,8]];
var arr2 = [[8,7,3,0,0,7,0,9,0],[0,9,0,0,3,0,0,0,0],[0,0,0,0,0,0,4,0,6],[0,0,0,0,3,9,7,6,0],[9,6,0,5,0,7,0,8,1],[0,6,4,6,8,0,0,0,0],[5,0,1,0,0,0,0,0,0],[0,0,0,0,5,0,3,7,0],[1,6,0,7,0,0,1,0,8]];

arr1 = JSON.stringify( arr1 );
arr2 = JSON.stringify( arr2 );
var temp = ''; // this object will hold the XOR result

console.log( arr1 );
console.log( arr2 );

for( var i=0; i < arr1.length; i++ ){
    if( arr1[i] == '[' || arr1[i] == ']' || arr1[i] == ',' )
        temp += arr1[i];
    else
        temp += arr1[i] == arr2[i] ? 0 : 1;
}

console.log( temp );

what are your thoughts of this method? better for performance I believe.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:33:33+00:00Added an answer on May 23, 2026 at 9:33 am

    Here’s a version that works for any type of object:

    var arr1 = [[8,0,3,0,0,7,0,9,0],[0,9,0,0,3,0,0,0,0],[0,0,0,0,0,0,4,0,6],[0,0,0,0,3,9,7,6,0],[9,6,0,5,0,7,0,8,1],[0,7,4,6,8,0,0,0,0],[5,0,1,0,0,0,0,0,0],[0,0,0,0,5,0,0,7,0],[0,6,0,7,0,0,1,0,8]];
    var arr2 = [[8,0,3,0,0,7,0,9,0],[0,9,0,0,3,0,0,0,0],[0,0,0,0,0,0,4,0,6],[0,0,0,0,3,9,7,6,0],[9,6,0,5,0,7,0,8,1],[0,7,4,6,8,0,0,0,0],[5,0,1,0,0,0,0,0,0],[0,0,0,0,5,0,0,7,0],[0,6,0,7,0,0,1,0,8]];
    
    alert(isEqual(arr1, arr2));
    
    function isEqual(obj1, obj2) {
        //make sure all keys are the same from obj1 -> obj2
        for (var key in obj1) {
            if (obj1[key] && ! obj2[key]) {
                return false;
            }
        }
    
        //make sure all keys are the same from obj2 -> obj1
        for (var key in obj2) {
            if (obj2[key] && ! obj1[key]) {
                return false;
            }
        }
    
        //make sure the key values themselves match
        for (var key in obj1) {
            var left = obj1[key];
            var right = obj2[key];
            if (left instanceof Function) {
                //don't compare these
                continue;
            } 
            if (left instanceof Object || left instanceof Array){
                if (! isEqual(left, right)) {
                    return false;
                }
            }
            else if (left != right) {
                return false;
            }
        }
    
        return true;
    }
    

    Or if you want to list the locations of all the differences, you can use something like:

    var arr1 = [[8,0,3,0,0,7,0,9,0],[0,9,0,0,3,0,0,0,0],[0,0,0,0,0,0,4,0,6],[0,0,0,0,3,9,7,6,0],[9,6,0,5,0,7,0,8,1],[0,7,4,6,8,0,0,0,0],[5,0,1,0,0,0,0,0,0],[0,0,0,0,5,0,0,7,0],[0,6,0,7,0,0,1,0,8]];
    var arr2 = [[8,0,3,0,0,7,0,9,0],[0,9,0,0,3,0,0,0,0],[1,0,0,0,1,1,4,0,6],[0,0,0,0,3,9,7,6,0],[9,6,0,5,0,7,0,8,1],[0,7,4,6,8,0,0,0,0],[5,0,1,0,0,0,0,0,0],[0,0,0,0,5,0,0,7,0],[0,6,0,7,0,0,1,0,8]];
    
    alert(listDifferences(arr1, arr2));
    
    function listDifferences(obj1, obj2, deltaList, keyPath) {
        if (! deltaList) {
            deltaList = [];
            keyPath = "";
        }
        //make sure all keys are the same from obj1 -> obj2
        for (var key in obj1) {
            if (obj1[key] && ! obj2[key]) {
                deltaList.push("obj1" + keyPath + "[" + key + "]");
            }
        }
    
        //make sure all keys are the same from obj2 -> obj1
        for (var key in obj2) {
            if (obj2[key] && ! obj1[key]) {
                deltaList.push("obj2" + keyPath + "[" + key + "]");
            }
        }
    
        //make sure the key values themselves match
        for (var key in obj1) {
            var left = obj1[key];
            var right = obj2[key];
            if (left instanceof Function) {
                //don't compare these
                continue;
            } 
            if (left instanceof Object || left instanceof Array){
                var startingLength = deltaList.length
                if (listDifferences(left, right, deltaList, keyPath + "[" + key + "]").length > startingLength) {
                    deltaList.push("obj1" + keyPath + "[" + key + "]");
                }
            }
            else if (left != right) {
                deltaList.push("obj1" + keyPath + "[" + key + "]");
            }
        }
    
        return deltaList;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my project i have one registration form which is developed in C#.net.to this
I have developed a application which caches multiple packages. The CLI interface for this
I have developed a simple library in Ruby and need to use this in
I have developed a control in C#. Among other things this control can popup
I have developed a Web service using WCF Service Application. This service application is
Before you answer this I have never developed anything popular enough to attain high
We have a desktop client application developed in Swing. This application interacts with backend
I have a pure C++ application developed using VC 6.0. I would like this
I have developed a Java EE web application. This application allows a user to
I have developed a webservice using Apache CXF ,which will be in production very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.