0

I am trying to convert a JSON string with the following data :

{
    "locations": [{
        "info": "7811 N. Octavia",
        "longitude": -87.8086439,
        "latitude": 42.0229656
    }, {
        "info": "PO Box 271743",
        "longitude": -73.087749,
        "latitude": 41.6032207
    }, {
        "info": "P.O. Box 269",
        "longitude": -86.7818523,
        "latitude": 34.0667074
    }]
}

to the following structure:

var locations = [
    ['Bondi Beach', -33.890542, 151.274856],
    ['Coogee Beach', -33.923036, 151.259052],
    ['Cronulla Beach', -34.028249, 151.157507],
    ['Manly Beach', -33.80010128657071, 151.28747820854187],
    ['Maroubra Beach', -33.950198, 151.259302]
];

Is there any way to achieve this using javascript?

5
  • 2
    What's the relation between the two? Commented Apr 8, 2019 at 10:36
  • 1
    yes, using Object.values and Array#map Commented Apr 8, 2019 at 10:37
  • you cannot as there is no relation in between. :) Commented Apr 8, 2019 at 10:41
  • Possible duplicate of How to convert array of objects into array of arrays Commented Apr 8, 2019 at 10:44
  • check my answer it might help you bro Commented Apr 8, 2019 at 11:00

6 Answers 6

5

You could get the values, as long as they have the right order.

var object = { locations: [{ info: "7811 N. Octavia", longitude: -87.8086439, latitude: 42.0229656 }, { info: "PO Box 271743", longitude: -73.087749, latitude: 41.6032207 }, { info: "P.O. Box 269", longitude: -86.7818523, latitude: 34.0667074 }] },
    locations = object.locations.map(Object.values);

console.log(locations);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

2 Comments

Could you explain how works .map(Object.values) if "values" is a method of Object. I'm lost in that part.
Array#map` takes a callback. Object.values works directly and has only one parameter. Without the calling parentheses, you could hand over this method like any other callback.
4

const obj = {
    "locations": [{
        "info": "7811 N. Octavia",
        "longitude": -87.8086439,
        "latitude": 42.0229656
    }, {
        "info": "PO Box 271743",
        "longitude": -73.087749,
        "latitude": 41.6032207
    }, {
        "info": "P.O. Box 269",
        "longitude": -86.7818523,
        "latitude": 34.0667074
    }]
};

locations = obj.locations.map(({info, longitude, latitude}) => [info, longitude, latitude]);

console.log(locations);

You can just map over the array and return the elements in the form you want to create a new array

Comments

1

You just need a forEach() on locations array:

var obj = {"locations":[{"info":"7811 N. Octavia","longitude":-87.8086439,"latitude":42.0229656},{"info":"PO Box 271743","longitude":-73.087749,"latitude":41.6032207},{"info":"P.O. Box 269","longitude":-86.7818523,"latitude":34.0667074}]};

var res = [];
obj.locations.forEach(function(location){
  var innerArr = [];
  innerArr.push(location.info);
  innerArr.push(location.longitude);
  innerArr.push(location.latitude);
  res.push(innerArr);
});
console.log(res);

Comments

1

You can use Array map

//Parsing JSON string to json object
var dataObj = JSON.parse('{"locations":[{"info":"7811 N. Octavia","longitude":-87.8086439,"latitude":42.0229656},{"info":"PO Box 271743","longitude":-73.087749,"latitude":41.6032207},{"info":"P.O. Box 269","longitude":-86.7818523,"latitude":34.0667074}]}')

//Using map to iterate over dataObj.locations and break the object into array based on keys
var locations = dataObj.locations.map(x => [x.info, x.longitude, x.latitude])

console.log(locations)

Comments

1

This code accept any object structure

let obj = {
  "locations": [{
      "info": "7811 N. Octavia",
      "longitude": -87.8086439,
      "latitude": 42.0229656
  }, {
      "info": "PO Box 271743",
      "longitude": -73.087749,
      "latitude": 41.6032207
  }, {
      "info": "P.O. Box 269",
      "longitude": -86.7818523,
      "latitude": 34.0667074
  }]
}

let arr = obj.locations.map(value => {
  let _arr = [];
  for (let key of Object.keys(value)) {
    _arr.push(value[key]);
  }
  return _arr
});

console.log(arr);

Comments

1

use reduce function to ittrate and put it value into a array and get the value.check the below code

let v ={
    "locations": [{
        "info": "7811 N. Octavia",
        "longitude": -87.8086439,
        "latitude": 42.0229656
    }, {
        "info": "PO Box 271743",
        "longitude": -73.087749,
        "latitude": 41.6032207
    }, {
        "info": "P.O. Box 269",
        "longitude": -86.7818523,
        "latitude": 34.0667074
    }]
}


let array = v.locations
let value = [];
let result = array.reduce( function( sample, data){
  sample = [];
	sample.push(data.info)
  sample.push(data.longitude)
  sample.push(data.latitude)
  value.push(sample);
  return sample
},[] )

console.log("Final Value:",value)

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.