How to create a third object from two objects using the key values in JavaScript?

In JavaScript, you can create a third object from two objects by mapping key values between them. This is useful when you have one object containing arrays of keys and another object with corresponding values.

Problem Statement

Given two objects where the first contains arrays of keys and the second contains key-value pairs, we need to calculate totals for each category:

const obj1 = {
  positive: ['happy', 'excited', 'joyful'],
  negative: ['depressed', 'sad', 'unhappy']
};

const obj2 = {
  happy: 6,
  excited: 1,
  unhappy: 3
};

console.log("Object 1:", obj1);
console.log("Object 2:", obj2);
Object 1: { positive: [ 'happy', 'excited', 'joyful' ], negative: [ 'depressed', 'sad', 'unhappy' ] }
Object 2: { happy: 6, excited: 1, unhappy: 3 }

Solution: Creating the Third Object

We'll create a function that iterates through the first object's keys, sums the corresponding values from the second object, and returns a new object with calculated totals:

const obj1 = {
  positive: ['happy', 'excited', 'joyful'],
  negative: ['depressed', 'sad', 'unhappy']
};

const obj2 = {
  happy: 6,
  excited: 1,
  unhappy: 3
};

const findPositiveNegative = (obj1 = {}, obj2 = {}) => {
  const result = {};
  
  for (let key of Object.keys(obj1)) {
    result[key] = obj1[key].reduce((acc, value) => {
      return acc + (obj2[value] || 0);
    }, 0);
  }
  
  return result;
};

const output = findPositiveNegative(obj1, obj2);
console.log(output);
{ positive: 7, negative: 3 }

How It Works

The function works by:

  • Iterating through obj1 keys: 'positive' and 'negative'
  • For each key: Using reduce() to sum values from obj2
  • Safe access: Using (obj2[value] || 0) to handle missing keys
  • Calculation breakdown:
    • positive: happy(6) + excited(1) + joyful(0) = 7
    • negative: depressed(0) + sad(0) + unhappy(3) = 3

Alternative Approach Using Object.entries()

const createThirdObject = (categories, values) => {
  return Object.entries(categories).reduce((result, [category, items]) => {
    result[category] = items.reduce((sum, item) => sum + (values[item] || 0), 0);
    return result;
  }, {});
};

const result = createThirdObject(obj1, obj2);
console.log("Alternative approach:", result);
Alternative approach: { positive: 7, negative: 3 }

Handling Edge Cases

// Test with missing values
const testObj1 = {
  category1: ['a', 'b', 'c'],
  category2: ['d', 'e']
};

const testObj2 = {
  a: 5,
  c: 2,
  e: 8
  // 'b' and 'd' are missing
};

const testResult = findPositiveNegative(testObj1, testObj2);
console.log("With missing values:", testResult);
With missing values: { category1: 7, category2: 8 }

Conclusion

This approach effectively creates a third object by mapping arrays of keys to their corresponding values. The reduce() method combined with safe property access ensures reliable calculation even when some keys are missing from the value object.

Updated on: 2026-03-15T23:19:00+05:30

254 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements