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 7188135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:00:03+00:00 2026-05-28T19:00:03+00:00

I need to compare two arrays. If I test if Object1==Object2 I am testing

  • 0

I need to compare two arrays. If I test if Object1==Object2 I am testing if the objects point to the same address in memory. I actually want to compare them based on their properties values. Let me show you what I mean.

I have a class in javascript:

function MyClass(var1,var2,var3)
{
   this.var1 = var1;
   this.var2 = var2;
   this.var3 = var3;
}

Now I will be able to create a new object of that class as:

var myNewObject = new MyClass(1,"hello",3);

and I will be able to access the properties of that object as:

myNewObject.var2 = "New value";

I have two arrays:

 var array1 = new Array();
 var array2 = new Array();

both arrays are populated with MyClass objects. In other words if array1 has 3 elements I know that they will be of type MyClass meaning I could do something like: array1[2].var1 = foo

*I will like to compare array1 with array2 based on the values of their properties *

for example if

   array1 = [ [1,"hello",4] , [0,"foo",5] ]; 

   array2 = [ [0,"foo",5555] [1111,"hello",4]];

   note: // [1,"hello",4] means a MyClass object with var1=1, var2="hello", var3 = 4

I will like to have a method that will tell me the changes. In this example var1 changed to 1111 and var3 changed from 5 to 5555.

I will probably be able to do it if I spend a lot of ours doing it. I am not to good at algorithms and maybe there is a simple way of doing it.


edit

I will like to know the changes that I will have to make to array1 in order to make it equal to array2.

  • 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-28T19:00:05+00:00Added an answer on May 28, 2026 at 7:00 pm

    “Making it equal” sounds like array1 = array2.slice(0), but that seems not to be what you want. So try the following:

    for (var i=0, l=Math.max(array1.length, array2.length); i<l; i++) {
        if (! array1[i]) {
            alert("You need to make a copy of "+array2[i]);
            continue;
        }
        if (! array2[i]) {
            alert("You need to delete "+array1[i]);
            continue;
        }
        for (var j in array2[i]) {
            if (array2.hasOwnProperty(i)) // needed only when MyClass has a prototype with enumerable properties
                if (array1[i][j] !== array2[i][j])
                    alert("You need to change "+j+" of "+array1[i]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to compare dozens of fields in two objects (instances of the same
I need to do operations like compare that two number arrays have the same
I need to compare between two int arrays of different size. It has to
I'm trying to compare two arrays both 2d, I need it only match when
basically I need a loop within a loop to compare two different arrays in
I want to compare two 1x3 arrays such as: if output[x][y] != [150,25,75] (
From two unequal arrays, i need to compare & delete based on the last
I want to compare two arrays and then order one of them based on
I have two arrays I need to compare.... $Drink Array ( [0] => Drink
I need to compare two Date s (e.g. date1 and date2 ) and come

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.