Javascript 如何从红色节点的json中提取多个值
我试图从多个JSON数组中提取多个值来创建饼图 下面的函数只能获取一个值,但我希望从这些数组中获取所有值。例如,tag应该返回“untaged”、“HK_Online”、“HK_Order/Change address”,但此时它只返回“HK_Online” 我的功能节点Javascript 如何从红色节点的json中提取多个值,javascript,node-red,Javascript,Node Red,我试图从多个JSON数组中提取多个值来创建饼图 下面的函数只能获取一个值,但我希望从这些数组中获取所有值。例如,tag应该返回“untaged”、“HK_Online”、“HK_Order/Change address”,但此时它只返回“HK_Online” 我的功能节点 var value = msg.payload.lines[0][1].value; var tag = msg.payload.lines[1][0].value; msg.topic = tag; msg.payload
var value = msg.payload.lines[0][1].value;
var tag = msg.payload.lines[1][0].value;
msg.topic = tag;
msg.payload = value;
return msg;
JSON数据
{
"axes": {
"x": [
{ "name": "Tag","type": "string" },
{ "name": "Total","type": "number" },
{ "name": "Percentage","type": "percent" },
{ "name": "Delta", "type": "delta" }
]
},
"lines": [
[
{ "type": "string","value": "Untagged" },
{ "type": "number","value": 1 },
{ "type": "percent","value": 20 },
{ "type": "delta", "value": 100 }
],
[
{ "type": "string","value": "HK_Online" },
{ "type": "number","value": 4 },
{ "type": "percent","value": 80 },
{ "type": "delta","value": 100 }
],
[
{ "type": "string","value": "HK_Order/Change address" },
{ "type": "number","value": 1 },
{ "type": "percent","value": 20 },
{ "type": "delta","value": 100 }
]
]
}
可以使用map方法从lines数组创建新数组
let lines = msg.payload.lines
let tag = lines.map(item => item[0].value)
let value = lines.map(item => item[1].value)
谢谢,我设法让它工作,但图表节点返回一个“坏数据注入”节点。我想知道这是为什么。