title: Creating new array object from existing API object
body: <p>I am trying to filter out the &quot;Country&quot; key values in an array.
  Below is the JSON structure.</p>
  <pre><code>let apidata = [
      {
          &quot;Brand&quot;:&quot;Puma&quot;,
          &quot;id&quot;: &quot;c5d58bef-f1c2-4b7c-a6d7-f64df12321bd&quot;,
          &quot;Country&quot;: [
              {
                  &quot;City&quot;: &quot;Mumbai&quot;,
                  &quot;Location&quot;: &quot;Andheri&quot;,
              },{
                  &quot;City&quot;: &quot;Bangalore&quot;,
                  &quot;Location&quot;: &quot;MG Road&quot;,
              },
          ],
          &quot;Shipping&quot;: [
              {
                  &quot;pincode&quot;:&quot;112144&quot;,
                  &quot;shipping&quot;:&quot;yes&quot;,
              },{
                  &quot;pincode&quot;:&quot;111451&quot;,
                  &quot;shipping&quot;:&quot;No&quot;,
              },
          ]
      },{
          &quot;Brand&quot;:&quot;adidas&quot;,
          &quot;id&quot;: &quot;c5d58bef-f1c2-4b7c-a6d7-f64df12321xv&quot;,
          &quot;Country&quot;: [
              {
                  &quot;City&quot;: &quot;Chennai&quot;,
                  &quot;Location&quot;: &quot;Marina&quot;,
              },{
                  &quot;City&quot;: &quot;Gurgaon&quot;,
                  &quot;Location&quot;: &quot;Huda City&quot;,
              },
          ],
          &quot;Shipping&quot;: [
              {
                  &quot;pincode&quot;:&quot;114144&quot;,
                  &quot;shipping&quot;:&quot;yes&quot;,
              },{
                  &quot;pincode&quot;:&quot;111414&quot;,
                  &quot;shipping&quot;:&quot;No&quot;,
              },
          ]
      },
  ]
  </code></pre>
  <p>The expected output should be:</p>
  <pre><code>b = [
          {
              &quot;City&quot;: &quot;Mumbai&quot;,
              &quot;Location&quot;: &quot;Andheri&quot;,
              &quot;uid&quot;: 0
          },
          {
              &quot;City&quot;: &quot;Bangalore&quot;,
              &quot;Location&quot;: &quot;MG Road&quot;,
              &quot;uid&quot;: 1
          },
          {
              &quot;City&quot;: &quot;Chennai&quot;,
              &quot;Location&quot;: &quot;Marina&quot;,
              &quot;uid&quot;: 0
          },
          {
              &quot;City&quot;: &quot;Gurgaon&quot;,
              &quot;Location&quot;: &quot;Huda City&quot;,
              &quot;uid&quot;: 1
          }
      ]
  </code></pre>
  <p><strong>Below is the code which I was trying to implement</strong></p>
  <pre><code>let b = [];
  apidata.map((ele) =&gt; {
      return Object.keys(ele).filter(key =&gt; key === &quot;Country&quot;).reduce((obj, ele1, index) =&gt; {
        // debugger;
          if(b.length == 0)
              b = ele[ele1].map((i,index)=&gt;({...i,&quot;uid&quot;:index}));
          else
               b = [...b, ...ele[ele1].map((i,index)=&gt;({...i,&quot;uid&quot;:index}))]  ;
          
          return b
          
      },{})
  })
  </code></pre>
  <p>I used filter concept to filter out the keys and combined with reduce to create a new object. But I am not able to get the output required.</p>
  <p>Can someone pls help me where I am making the mistake?</p>