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

  • Home
  • SEARCH
  • 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 7623379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:41:33+00:00 2026-05-31T04:41:33+00:00

var array1 = [{ name : foo , age : 22}, { name :

  • 0
var array1 = [{ "name" : "foo" , "age" : "22"}, { "name" : "bar" , "age" : "33"}];
var array2 = [{ "name" : "foo" , "age" : "22"}, { "name" : "buz" , "age" : "35"}];

What’s the fastest way to have (no duplicates, name is the identifier):

[{ "name" : "foo" , "age" : "22"}, { "name" : "bar" , "age" : "33"}, { "name" : "buz" , "age" : "35"}];

With and without jquery if possible.

  • 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-31T04:41:34+00:00Added an answer on May 31, 2026 at 4:41 am

    Here’s a general purpose function that would merge an arbitrary number of arrays, preventing duplicates of the passed in key.

    As it is merging, it creates a temporary index of the names used so far and only merges new elements that have a unique name. This temporary index should be much faster than a linear search through the results, particularly as the arrays get large. As a feature this scheme, it filters all duplicates, even duplicates that might be in one of the source arrays.

    If an element does not have the keyName, it is skipped (though that logic could be reversed if you want depending upon what error handling you want for that):

    var array1 = [{ "name" : "foo" , "age" : "22"}, { "name" : "bar" , "age" : "33"}];
    var array2 = [{ "name" : "foo" , "age" : "22"}, { "name" : "buz" , "age" : "35"}];
    
    function mergeArrays(keyName /* pass arrays as additional arguments */) {
        var index = {}, i, len, merge = [], arr, name;
    
        for (var j = 1; j < arguments.length; j++) {
            arr = arguments[j];
            for (i = 0, len = arr.length; i < len; i++) {
                name = arr[i][keyName];
                if ((typeof name != "undefined") && !(name in index)) {
                    index[name] = true;
                    merge.push(arr[i]);
                }
            }
        }
        return(merge);
    }
    
    var merged = mergeArrays("name", array1, array2);
    
    // Returns:
    // [{"name":"foo","age":"22"},{"name":"bar","age":"33"},{"name":"buz","age":"35"}]
    

    You can see it work here: http://jsfiddle.net/jfriend00/8WfFW/

    When this algorithm is run against the Matt algorithm in jsperf using larger arrays, this algorithm is around 20x faster:

    enter image description here

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

Sidebar

Related Questions

I have two JavaScript arrays: var array1 = [Vijendra,Singh]; var array2 = [Singh, Shakya];
I have an array of JSON objects like so: var myArray = [ {name:'foo',number:2},
i have this JSON: var projects_array = new Array( {name:myName1, id:myid1, index:1}, {name:myName2, id:myid2,
I have the following string arrays: var array1 = new String[] { A, B,
Consider: class Foo { public string Name{get;set;} public string[] WebSites{get;set;} } I have a
I have the following two classes. public class Foo { public Bar Args {
I have a model public class Foo{ public int Id{get;set;} public string Name {get;
I have a simple model object with one array property. Example: {name: 'Foo', tags
Given var myHash = new Array(); myHash['key1'] = { Name: 'Object 1' }; myHash['key2']
Given the following array of json objects: var items = [ {id:1, parentId:0, name:'item1'},

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.