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

Before you answer this I have never developed anything popular enough to attain high
I have developed about 300 Applications which I would like to provide with multi-language
I have developed a JSP web application which, on every request, spawns a new
I have developed a facebook application in which I am sending data from Flash
I have developed a web based application which allows a client/user to upload a
I have developed a web dashboard which has a structure of controls embedded inside
I just finished reading this post: https://developer.yahoo.com/performance/rules.html#flush and have already implemented a flush after
This is my first time with Web services. I have to develop web services
I have developed some classes with similar behavior, they all implement the same interface.
I have developed some custom DAO-like classes to meet some very specialized requirements for

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.