title: combinig an object array into one using javascript
body: <p>Hi i have an array of objects using which i'll have to create one common object
  below is my array</p>
  
  <pre><code> [
      {
      "id": "mapbox-places.16822",
      "bounds": [
        -77.89820379123734,
        41.0650562721515,
        -77.51623302327094,
        41.2527616498374
      ],
      "lon": -77.704974,
      "lat": 41.158976,
      "name": "Beech Creek",
      "type": "city"
       },
       {
      "id": "province.2184819983",
      "bounds": [
        -80.519851,
        39.719798999999995,
        -74.68950199999999,
        42.51607199999999
      ],
      "lon": -77.725902,
      "lat": 41.132832,
      "name": "Pennsylvania",
      "type": "province"
       },
       {
      "id": "country.4150104525",
      "bounds": [
        -179.23108600000003,
        18.865459999999985,
        179.85968099999997,
        71.441059
      ],
      "lon": -99.041505,
      "lat": 37.940711,
      "name": "United States",
      "type": "country"
    }
  ]
  </code></pre>
  
  <p>below is the code that i tried where result contains the above array</p>
  
  <pre><code> var finalResult = {};
      var resultarray=[];
      for (var j = 0; j &lt; result.length; j++) {
  
  
          var latitude = null,
              longitude = null,
              name = null,
              type = null,
              state = null,
              stateCode = null,
              zipcode = null,
              streetName = null,
              streetNumber = null,
              countryCode = null;
  
          if (result[j].lat != undefined)
              latitude = result[j].lat;
          if (result[j].lon != undefined)
              longitude = result[j].lon;
          if (result[j].name != undefined)
              name = result[j].name;
          if (result[j].type != undefined)
              type = result[j].type;
  
          if (type == 'country') {
              finalResult.country = name
              finalResult.city = null
              finalResult.streetName = null
              finalResult.state = null
  
          }
          else if (type == 'city') {
              finalResult.city = name
              finalResult.country = null
              finalResult.streetName = null
              finalResult.state = null
  
          }
          else if (type == 'street' || type == 'address') {
              finalResult.streetName = name
              finalResult.country = null
              finalResult.city = null
              finalResult.state = null
  
          }
          else if (type == 'province') {
              finalResult.state = name
              finalResult.country = null
              finalResult.city = null
              finalResult.streetName = null
  
          }
  
          else {
              finalResult.country = null
              finalResult.city = null
              finalResult.streetName = null
              finalResult.state = null
  
          }
           finalResult.latitude = latitude,
              finalResult.longitude = longitude,
              finalResult.state = state,
              finalResult.stateCode = stateCode,
              finalResult.zipcode = zipcode,
              finalResult.streetNumber = streetNumber,
              finalResult.countryCode = countryCode
          resultarray.push(finalResult)
           }
  </code></pre>
  
  <p>And the output i received is</p>
  
  <pre><code> [{"city":null,"country":"United States","streetName":null,"state":null,"latitude":37.940711,"longitude":-99.041505,"stateCode":null,"zipcode":null,"streetNumber":null,"countryCode":null},{"city":null,"country":"United States","streetName":null,"state":null,"latitude":37.940711,"longitude":-99.041505,"stateCode":null,"zipcode":null,"streetNumber":null,"countryCode":null},{"city":null,"country":"United States","streetName":null,"state":null,"latitude":37.940711,"longitude":-99.041505,"stateCode":null,"zipcode":null,"streetNumber":null,"countryCode":null}]
  </code></pre>
  
  <p>I dont know where i'm going wrong .The correct format that i need is as below</p>
  
  <pre><code> [
      {
      "city": "Beech Creek",
      "country": "United States",
      "streetName": null,
      "state": "Pennsylvania",
      "latitude":41.132832,
      "longitude": -77.725902,
      "stateCode": null,
      "zipcode": null,
      "streetNumber": null,
      "countryCode": null
    }
    ]
  </code></pre>
  
  <p>I'm stuck here any help regarding this will be much helpful</p>