Skip to content Skip to sidebar Skip to footer

How To Convert Nested Object Into Array Of Array

Hi i have a situation where i want to convert array of object into array of array here is my targeted array of objects which looks like this (32) [Object, Object, Object, Object, O

Solution 1:

You could map the result of Object.values.

For older user agents, you could use a polyfill.

var array = [{ location: "MUGABALA  KOLAR ROAD", latitude: 13.108435679884, longitude: 77.890262391016 }, { location: "pune", latitude: 18.6202008, longitude: 73.7908073 }, { location: "RAJARAJESHWARI NAGAR BANGLORE", latitude: 12.901112992767, longitude: 77.5037757 }],
    result = array.map(Object.values);
    
console.log(result);
.as-console-wrapper { max-height: 100%!important; top: 0; }

Solution 2:

You can do it using Object.values() and Array.prototype.map():

var results = targetObject.map(function(obj){
      returnObject.values(obj);
});
console.log(results);

Demo:

targetObject = [

{location: "MUGABALA  KOLAR ROAD", latitude: 13.108435679884, longitude: 77.890262391016},

{location: "pune", latitude: 18.6202008, longitude: 73.7908073},

{location: "RAJARAJESHWARI NAGAR BANGLORE", latitude: 12.901112992767, longitude: 77.5037757}

];

var results = targetObject.map(function(obj){
      returnObject.values(obj);
});
console.log(results);

Object.values():

The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

Post a Comment for "How To Convert Nested Object Into Array Of Array"