0

I have following codes:

for (lo = 0; lo < newobj.length; lo++) {
  lon = '{"lons":' + newobj[lo].longitude;
  lat = ',"lats":' + newobj[lo].latitude + '}';
  var string = '[' + lon + lat + ']';
  var obj = JSON.parse(string);
}

Thereafter, I do not know how to continue to get the expected outcome.

Example of data on the API. I actually just need the latitude and longitude to plot a billboard on cesium.

[

    {"description": "aaa", "address": "bbb", "latitude": "1.34791838898824", "longitude": "103.8487501254"},
    {"description": "ddd", "address": "ccc", "latitude": "1.37026158388488", "longitude": "103.839467898898"},
    ....
] 

Expected outcome:

var dots=[{lon:103.84606,lat:1.3694},{lon:103.8447,lat:1.3697},…]
3
  • post your actual data. Commented Oct 18, 2019 at 5:37
  • can you post an example of your newobj array Commented Oct 18, 2019 at 5:40
  • Example of data on the API. I actually just need the latitude and longitude to plot a billboard on cesium. [{"description":"aaa","address":"bbb","latitude":"1.34791838898824","longitude":"103.8487501254"},{"description":"ddd","address":"ccc","latitude":"1.37026158388488","longitude":"103.839467898898"},....] Commented Oct 18, 2019 at 5:52

5 Answers 5

1

Why are you converting all in strings?

You can simply do it like this

var dots = [];
for (lo = 0; lo < newobj.length; lo++) {
  dots.push({
    'lons': newobj[lo].longitude,
    'lats': newobj[lo].latitude
  });
}

Sign up to request clarification or add additional context in comments.

Comments

1

use Array.map : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

var newobj = [ {longitude : 103.84606 , latitude: 1.3694 }
             , {longitude : 103.8447 , latitude: 1.3697 }
             ]
         
var dots = newobj.map(elm=>({ lon: elm.longitude, lat: elm.latitude }) )

console.log( dots ) 

If you absolutely want a loop:

var newobj = [ {longitude : 103.84606 , latitude: 1.3694 }
             , {longitude : 103.8447 , latitude: 1.3697 }
             ]
             
var dots = []
for (let elm of newobj) {
  dots.push({ lon: elm.longitude, lat: elm.latitude })
}

console.log( dots ) 

Comments

0

Try this :

let dots= [];
for (lo = 0; lo < newobj.length; lo++) {
        lon= {
          "lons":newobj[lo].longitude,
        "lats":newobj[lo].latitude
};
dots.push(lon);
}

Comments

0

Try this.

var dots = [];
for (lo = 0; lo < newobj.length; lo++) {
    var obj = {};
    obj.lon = newobj[lo].longitude;
    obj.lat = newobj[lo].latitude;
    dots.push(obj);
}

Comments

0
const newObjArr = []

for (lo = 0; lo < newobj.length; lo++) {
    newObjArr.push([{lons: newobj[lo].longitude, lat: newobj[lo].latitude}])
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.