简体   繁体   English

将 Javascript Object 转换为对象数组以在 d3.js 中使用

[英]Convert Javascript Object to Array of Objects to use in d3.js

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.我正在将 d3 迁移到较新的版本 7.6.1,现在我使用的版本是 5.15,它有一种方法是d3.entries ,在版本 7.6.1 中它已被弃用。

As far as i know this d3.entries is used to convert object to array of object for example -据我所知,这个 d3.entries 用于将 object 转换为 object 的数组,例如 -

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() -但是当我升级我的 d3 版本时,这个 d3.entries() function 不存在所以我使用了 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.但我仍然没有得到所需的 output。

Use this code:使用此代码:

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

You can do this你可以这样做

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

let arr = []

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM