Home > Enterprise >  Convert Javascript Object to Array of Objects to use in d3.js
Convert Javascript Object to Array of Objects to use in d3.js

Time:11-15

I am migrating d3 to newer version 7.6.1, now the version which i am using is 5.15 and it has one method which is d3.entries and in version 7.6.1 it is deprecated.

As far as i know this d3.entries is used to convert object to array of object for example -

chart.data = function(value,newName,newColor,sorted) {
       varColor=newColor;
       varSorted=sorted;
       displayData = d3.entries(value); //version 5.15
       console.log("-----");
       console.log(displayData);
       assignedName = newName;
       return chart;
}
{Metrics: 404, Asset: 492, B7: 84} to [{'Metrics',404},  {'Asset': 492}, {'B7': 84}]

but when i upgrade my d3 version this d3.entries() function is not there so i used Object.entries() -

chart.data = function(value,newName,newColor,sorted) {
       varColor=newColor;
       varSorted=sorted;
       displayData = Object.entries(value); //version 7.6
       console.log("-----");
       console.log(displayData);
       assignedName = newName;
       return chart;
}
My Output is - 
[['Metrics',404],  ['Asset': 492], ['B7': 84]]

but still i am not getting the desired output.

CodePudding user response:

Use this code :

Array.from(Object.entries(value) , d=>{
  return{
    d[0] : d[1]
  }
 })

CodePudding user response:

You can do this

let obj = {Metrics: 404, Asset: 492, B7: 84};

let arr = []

for(let item in obj){
  arr.push({[item]:obj[item]})
}
  • Related