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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:56:11+00:00 2026-06-07T08:56:11+00:00

Is it possible to get the difference of an associative array and a regular

  • 0

Is it possible to get the difference of an associative array and a regular array in Javascript?

Ex.

array1 = [5, 1, 3];
array2 = [1 => 15, 2 => 20, 3 => 10, 4 => 5, 5 =>50 ];

The difference would be…

diff = [2 => 20, 4=> 5];
  • 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-06-07T08:56:13+00:00Added an answer on June 7, 2026 at 8:56 am

    I assume your question just had a small typo in your declaration of array2. This is not a big deal.

    Anyway, here is a bit of hack, but it gives you what you want:

    array1 = [5, 1, 3];
    array2 = {1: 15, 2: 20, 3: 10, 4: 5, 5: 50};
    
    var result = {};
    for (var i in array2) {
        if (array1.indexOf(+i) < 0) {
            result[i] = array2[i];
        }
    }
    
    alert(JSON.stringify(result));
    

    Working example

    My hack is the +i in the indexOf call, because the properties of your “associative array” are strings, but your array1 contains numbers. The unary + produces a number from a string. Kind of hackish but it is idiomatic an accepted JavaScript practice.

    ADDENDUM

    As RobG points out, indexOf is an ES5 method, so if your JavaScript engine is ES3 or below, you will need to implement indexOf on your own. An example of how to do this is at MDN. Alternatively, you can just do the equivalent of indexOf by searching the array on your own.

    ​

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

does it possible to get difference between images in OpenCV of pixel in Matrix
Possible Duplicate: Calculating the Difference Between Two Java Date Instances How do I get
Possible Duplicate: Get variable name. javascript “reflection” Is there a way to know the
Possible Duplicate: Date Difference in php? Compare 2 timestamps in PHP and get the
Possible Duplicate: How to Get time difference in iPhone I´m getting date and time
Possible Duplicate: jQuery ajax() vs get()/post() what is the difference between jQuery ajax() and
Is it possible to calculate difference between two timestamps in MySQL and get output
Possible Duplicate: What's the difference between a URI and a URL? Just to get
Possible Duplicate: Get difference from 2 lists. Python I have two lists rt =
I would like to know if it is possible to get the coordinates, left

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.