brown26
brown26

Reputation: 89

how to remove key keep the value in array of object javascript

I have this object:

[
   {
     "latitude": "113.232160114",
     "longitude": "-1.786978559"
   },
   {
     "latitude": "113.211955085",
     "longitude": "-1.790031776"
   }
]

is there any possible way to make it look like this using JavaScript?

[
   [
      113.232160114,
      -1.786978559
   ],
   [
      113.211955085,
      -1.790031776
   ]
]

Upvotes: 1

Views: 802

Answers (2)

Amadan
Amadan

Reputation: 198324

Yes, with one single line, and two built-in functions: Array.prototype.map produces an array obtained by applying a function to each element of an array, and Object.values produces a list of enumerable properties of an object.

const data = [
   {
     "latitude": "113.232160114",
     "longitude": "-1.786978559"
   },
   {
     "latitude": "113.211955085",
     "longitude": "-1.790031776"
   }
];

const result = data.map(Object.values);
console.log(result);

Upvotes: 4

Andrew Parks
Andrew Parks

Reputation: 8087

const a = [
  { latitude: '113.232160114', longitude: '-1.786978559' },
  { latitude: '113.211955085', longitude: '-1.790031776' }
];
console.log(a.map(({latitude:i, longitude:j})=>[i,j]));

Upvotes: 1

Related Questions