3 ways to merge 2 JSON objects with the same key in JavaScript

Marika Lam
2 min readSep 21, 2022

The 3 ways to merge 2 JSON objects are

  1. Nested for loop
  2. Map and Object.assign
  3. concat()

1. Nested for loop

<!DOCTYPE html><html><body><script type="text/javascript">var g = [{ id: 36, name: 'AAA', goal: 'yes' },{ id: 40, name: 'BBB', goal: 'yes' },{ id: 57, name: 'CCC', goal: 'yes' },{ id: 14, name: 'DDD', goal: 'yes' },{ id: 39, name: 'EEE', goal: 'yes' },{ id: 37, name: 'FFF', goal: 'yes' },{ id: 59, name: 'GGG', goal: 'yes' },{ id: 50, name: 'III', goal: 'yes' },{ id: 43, name: 'HHH', goal: 'yes' },{ id: 35, name: 'JJJ', goal: 'yes' }],c = [{ id: 36, name: 'AAA', circle: 'yes' },{ id: 40, name: 'BBB', circle: 'yes' },{ id: 57, name: 'CCC', circle: 'yes' },{ id: 42, name: 'ZZZ', circle: 'yes' },{ id: 14, name: 'DDD', circle: 'yes' },{ id: 39, name: 'EEE', circle: 'yes' },{ id: 37, name…

--

--