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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:38:06+00:00 2026-06-15T15:38:06+00:00

I have two arrays of objects: var arOne = [ {key: ‘alpha’, value: 5},

  • 0

I have two arrays of objects:

var arOne = [
    {key: 'alpha', value: 5},  
    {key: 'beta', value: 11},  
    {key: 'gamma', value: 15},  
    {key: 'delta', value: 12},  
    {key: 'epsilon', value: 55}  
    {key: 'pony', value: 101}
]

var arTwo = [
    {key: 'alpha', value: 5.5},  
    {key: 'beta', value: 11.5},  
    {key: 'gamma', value: 15.5},  
    {key: 'psi', value: 12.5},  
    {key: 'omega', value: 55.5}  
]

I need to merge values into one array of arrays.

Case where the keys match: create an array with the key and append the value from arTwo to arOne.

Case where the keys do not match: if the key exists in arOne, I include the value from arOne and a 0 for arTwo. If the key exists in arTwo, I include a 0 for arOne and the value from arTwo.

Note that arOne and arTwo may be different sizes (see the pony key in arOne).

This is what the result should look like:

var result = [
    ['alpha', 5, 5.5],  
    ['beta', 11, 11.5],  
    ['gamma', 15, 15.5],  
    ['delta', 12, 0],  
    ['epsilon', 55, 0],  
    ['pony', 101, 0],
    ['psi', 0, 12.5],  
    ['omega', 0, 55.5],  
]

I’ve been staring at this all day and scratched all my attempts. Any thoughts?

  • 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-15T15:38:07+00:00Added an answer on June 15, 2026 at 3:38 pm
    function combine(obj1, obj2){
        var arr = [], k, i;
        for (k = 0; k < obj1.length; ++k){
            arr.push([obj1[k].key, obj1[k].value]);
        }
        for (k = 0; k < obj2.length; ++k){
            var exists = false;
            for (i = 0; i < arr.length; ++i){
                if(arr[i][0] === obj2[k].key){
                   arr[i].push(obj2[k].value);
                   var exists = true; 
                   break;
                }
            }
            if (!exists){
                arr.push([obj2[k].key, 0, obj2[k].value]);
            }
        }
        for (i = 0; i < arr.length; ++i){
            if (arr[i].length === 2){
                arr[i].push(0);
            }
        }
    
        return arr;
    }
    

    Call like:

    var myNewArray = combine(arOne, arTwo);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Objects vs arrays in Javascript for key/value pairs I have a variable
I have two arrays of objects: var a = [ {'id': 20}, {'id': 15},
I have two arrays in JavaScript: var xcoord = []; and var ycoord =
I have two arrays: var columns = ["Date", "Number", "Size", "Location", "Age"]; var rows
I have a situation where I have two arrays of objects. I want select
I have two JavaScript objects: var attributes1 = { type: text/css, rel: stylesheet, href:
I have an array of objects: var array = [(id, name, value),(id, name, value)];
So I have two objects. class Product extends DataMapper { var $has_many = array('productrating');
I have the following issue: A java object contains two arrays of core datastore
I have two arrays, @names and @employees , that are filled with strings that

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.