Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to flatten JS object (keys and values) to a single depth array

I have written this small function to get all keys and values of an object and store them into an array. The object might contain arrays as values...

Object { 0: [1,2,3,4] } to [0,1,2,3,4] converting all elements to integers

I wonder whether there is a faster/cleaner way to do so:

function flattenObject(obj) {     // Returns array with all keys and values of an object     var array = [];     $.each(obj, function (key, value) {         array.push(key);         if ($.isArray(value)) {             $.each(value, function (index, element) {                 array.push(element);             });         }         else {             array.push(value);         }     });      return array } 
like image 318
ddomingo Avatar asked May 23 '17 11:05

ddomingo


People also ask

How do you flatten an object to an array?

Use the concat() Method to Flatten an Object in JavaScript Object. keys() will return an array with all the other objects inside, and concat() will merge these objects. All the objects are flattened in a single array, and the nested object is a member of that merged array.

How do you turn an object into an array?

To convert an object to an array you use one of three methods: Object. keys() , Object. values() , and Object. entries() .

How do you flatten data in JavaScript?

Flatten a JSON object: var flatten = (function (isArray, wrapped) { return function (table) { return reduce("", {}, table); }; function reduce(path, accumulator, table) { if (isArray(table)) { var length = table.

How do you flatten a nested array of objects?

flat()” method is embedded in ES6 that enables you to “flatten” a nested JavaScript Array. This method returns a new array in which all of the elements of sub-arrays are concatenated according to the specified depth. Here, the “Array” object will invoke the “flat()” method while passing “depth” as an argument.


2 Answers

I wanted to flatten my deep object to one level depth. None of the above solutions worked for me.

My input:

{     "user": {         "key_value_map": {             "CreatedDate": "123424",             "Department": {                 "Name": "XYZ"             }         }     } } 

Expected output:

{     "user.key_value_map.CreatedDate": "123424",     "user.key_value_map.Department.Name": "XYZ" } 

Code that worked for me:

function flattenObject(ob) {     var toReturn = {};      for (var i in ob) {         if (!ob.hasOwnProperty(i)) continue;          if ((typeof ob[i]) == 'object' && ob[i] !== null) {             var flatObject = flattenObject(ob[i]);             for (var x in flatObject) {                 if (!flatObject.hasOwnProperty(x)) continue;                  toReturn[i + '.' + x] = flatObject[x];             }         } else {             toReturn[i] = ob[i];         }     }     return toReturn; } 
like image 161
Muthukrishnan Avatar answered Oct 13 '22 11:10

Muthukrishnan


You could just concat all keys and values. (It does not solve the type casting to number for keys.)

var object =  { 0: [1, 2, 3, 4] },      result = Object.keys(object).reduce(function (r, k) {          return r.concat(k, object[k]);      }, []);        console.log(result);
like image 35
Nina Scholz Avatar answered Oct 13 '22 11:10

Nina Scholz