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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:38:14+00:00 2026-05-29T07:38:14+00:00

I have two or more javascript objects. I want to merge them adding values

  • 0

I have two or more javascript objects. I want to merge them adding values of common properties and then sort them in descending order of values.

e.g.

var a = {en : 5,fr: 3,in: 9}
var b = {en: 8,fr: 21,br: 8}

var c = merge(a,b)

c should then be like this:

c = {
fr: 24,
en: 13,
in:9,
br:8
} 

i.e. both objects are merge, values of common keys are added and then keys are sorted.

Here’s what I’ve tried:

var a = {en : 5,fr: 3,in: 9}
var b = {en: 8,fr: 21,br: 8}
c = {}

// copy common values and all values of a to c
for(var k in a){
  if(typeof b[k] != 'undefined'){  
    c[k] = a[k] + b[k]  
  }
  else{ c[k] = a[k]}
}

// copy remaining values of b (which were not common)
for(var k in b){
 if(typeof c[k]== 'undefined'){
  c[k] = b[k]
 }
} 

// Create a object array for sorting
var arr = [];

for(var k in c){
 arr.push({lang:k,count:c[k]})
}

// Sort object array
arr.sort(function(a, b) {
   return b.count - a.count;
})

but I dont think its good. So many loops 🙁 It would be nice if someone can provide a less messy and good code.

  • 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-29T07:38:15+00:00Added an answer on May 29, 2026 at 7:38 am

    It is not possible to sort the properties of an object, you can however sort an array:

    var merged = $.extend({}, a);
    for (var prop in b) {
        if (merged[prop]) merged[prop] += b[prop];
        else merged[prop] = b[prop];
    }
    // Returning merged at this point will give you a merged object with properties summed, but not ordered.
    var properties = [];
    for (var prop in merged) {
        properties.push({
            name: prop,
            value: merged[prop]
        });
    }
    return properties.sort(function(nvp1, nvp2) {
        return nvp1.value - nvp2.value;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two or more tables which hold values under the same column name.
I have a JavaScript function that takes two required parameters and then arbitrarily many
I have two arrays of JavaScript Objects that I'd like to compare to see
suppose you have two (or more) classes with private member vectors: class A {
Is this possible to have two (or more) different kinds of cells to be
I have a two or more forms on my page in a row. I'm
I have two arrays, one is very large (more than million entries) and other
The problem: I often have to update two or more repositories: One for the
I have two models, Image and Tag . Each Image object can have more
I have been coming across these two words more often but i didn't see

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.