title: JSON - how to merge 2 data set using keys using javascript reduce()
body: <p>I have 2 sets of JSON data that I need to merge - merge <strong>productsBrands</strong> with <strong>Products</strong>, what is the best way to achieve this using javascript?</p>
  
  <p><strong>ProductBrands JSON data:</strong></p>
  
  <pre><code>const productBrands = {
    "Brand A": {
      "Brand D": {},
      "Brand E": {}
    },
    "Brand B": {
      "Brand F": {},
      "Brand G": {}
    }
  };
  </code></pre>
  
  <p><strong>Products JSON data:</strong></p>
  
  <pre><code>const products= [
    {
      "Brand A": {
        "BrandName": "Brand A",
        "HasChildBrands": {}
      },
      "Brand B": {
        "BrandName": "Brand B",
        "HasChildBrands": {}
      },
      "Brand C": {
        "BrandName": "Brand C",
      },
      "Brand D": {
        "BrandName": "Brand D",
      },
      "Brand E": {
        "BrandName": "Brand E",
      },
      "Brand F": {
        "BrandName": "Brand F",
      },
      "Brand G": {
        "BrandName": "Brand G",
      }
    }
  ];
  </code></pre>
  
  <p><strong>Expected Merged output:</strong></p>
  
  <pre><code>const products= [
    {
      "Brand A": {
        "BrandName": "Brand A",
        "HasChildBrands": {
          "BrandName": "Brand D",
        },
        {
          "BrandName": "Brand E",
        }
      },
      "Brand B": {
        "BrandName": "Brand B",
        "HasChildBrands": {
          "BrandName": "Brand F",
        },
        {
          "BrandName": "Brand G",
        }
      },
      "Brand C": {
        "BrandName": "Brand C",
      },
      "Brand D": {
        "BrandName": "Brand D",
      },
      "Brand E": {
        "BrandName": "Brand E",
      },
      "Brand F": {
        "BrandName": "Brand F",
      },
      "Brand G": {
        "BrandName": "Brand G",
      }
    }
  ];
  </code></pre>
  
  <p><strong>Below is what I have tried so far, but its still not what I expected:</strong></p>
  
  <pre><code>let products = data.reduce(function (acc, obj) {
    let item = Object.values(obj)[0]; //Get Indented Objects of Data
    let BrandNames = item.BrandName; //Obtain "Brand A", "Brand B", "Brand C" ... so on.
    let KeysForChildBrands = Object.keys(childBrands);
    if($.inArray(BrandNames, KeysForChildBrands) != -1){ //Check if Brand Name existed in ProductBrands data
      item['HasChildBrands'] = ProductBrands[BrandNames];
    }
    acc.push(obj);
    return acc;
  },[]);
  console.log(JSON.stringify(products)); //Got stuck here.
  </code></pre>