从其他对象返回对象数组

时间:2019-05-03 11:08:04

标签: javascript arrays ecmascript-6 es6-map

您好,我试图从对象中提取一些信息以创建图形,但是它返回的未定义对象是

{
"concepts": [
        {
            "id_cpt": "1",
            "fr_cpt": "Proche",  
        },
        {
            "id_cpt": "2",
            "fr_cpt": "Loin",  
        }{
            "id_cpt": "3",
            "fr_cpt": "Here",  
        },...
],
"arcs":  [
     {
       "idfrom":"1",
       "idto":"2"
     },
     {
       "idfrom":"3",
       "idto":"2"
     },....
]
}

我想使对象看起来像

const data = {
    nodes: [{ id: 'Proche' }, { id: 'Loin' },{ id: 'Here' } ...],
    links: [{ source: 'Proche', target: 'Loin' }, { source: 'Here', target: 'Loin' },...]
};

我要提取链接中的名称而不是id,但是对象弧仅在es6中具有id的代码,感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以使用for...of遍历concepts。填充nodes数组和map对象。 map对象具有id_cpt作为键和fr_cpt作为值。

{
  "1": "Proche",
  "2": "Loin",
  "3": "Here"
}

此对象可用于获取source的{​​{1}}和target值。然后遍历links并使用arcs对象创建links

这是一个片段:

map

相关问题