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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:44:35+00:00 2026-05-28T14:44:35+00:00

I have the following function which compares the first column value in two 2D

  • 0

I have the following function which compares the first column value in two 2D arrays.

var new_array = [['EMI_007','lion','cat'],
                 ['EMI_008','cat','dog']
                ];

var existing_array = [['EMI_002','cat','tiger'],
                     ['EMI_004','hen','pig'],
                     ['EMI_007','pigeon','hen'],
                     ['EMI_001','boar','tiger']
                ];

The value in new_array[i][0] is compared to the value in existing_array[j][0].

If they are the same, the entire row is replaced by the row in new_array, otherwise the new_array row is concatenated to the existing array.

In the above example, new_array is compared to existing_array.

EMI_007 exists in ‘existing_array’, so it’s replaced by ['EMI_007','lion','cat'] and [‘EMI_008′,’cat’,’dog’] is appended to the existing array.

I have created the following function but there seems to be an error.

If a row with an EMI id found in new_array does not exist in ‘existing_array’, it is inserted several times.

function concat_2D_array(existing_array, new_array){
    for (var i=0; i<new_array.length; i++) {
        for (var j=0; j<existing_array.length; j++) {
            if(new_array[i][0] == existing_array[j][0]){
            alert("ok,it already exists in the existing_array");
            existing_array.splice(j,1,new_array[i]);
            }
            else{
            alert("It is not in the existing array");
            // existing_array.concat(new_array[i]);
            }
        }
    }
    alert("returned existing_array"+existing_array)
    return existing_array;
}

Any help is most appreciated

  • 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-28T14:44:36+00:00Added an answer on May 28, 2026 at 2:44 pm

    The main issue with your code is that you are trying to add new item to existing array on each step and don’t leave the loop, once added.

    function concat_2D_array(existing_array, new_array){
        for (var i=0; i<new_array.length; i++) {
            var j = 0;
            while(j<existing_array.length){
                if(new_array[i][0] == existing_array[j][0]){
                    alert("ok,it already exists in the existing_array");
                    existing_array.splice(j,1,new_array[i]);
                    break;  // we found it, replaced it, leaving the loop
                } else if (j+1 >= existing_array.length){
                    alert("It is not in the existing array");
                    existing_array.splice(j+1,0,new_array[i]);
                    break;  // we didn't find it, added it, leaving the loop
                } else {
                    j++;
                }
            }
        }
        alert("returned existing_array"+existing_array)
        return existing_array;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following JS which compares credit card number length: validate: function ()
I have the following function which accepts text and a word count and if
I have the following function which sets up the GUI. public void go() {
I have the following function which works very well within a $(document).ready(function(){ $('.threadWrapper >
I have the following function which creates a std::vector of iterators into another container:
I have the following function in a PHP script, which returns and API call:
I have the following C++ function definition, which I am trying to call through
I have a for-loop which performs the following function: Take a M by 8
I have added the following fine function to my status bar to show which
All, I have the following array and function, which will create a multidimensional array

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.