使用循环填充对象值

发布于 2025-02-13 00:23:27 字数 2431 浏览 0 评论 0原文

我正在使用节点红色使用PLC的数据填充图表。数据以对象的形式出现,然后我将其转换为2D数组36个项目(对象具有36个元素):

for(var i in pressCurve){
    curveArray.push([i, pressCurve [i]]);

然后,要将此数据获取到图表上,我创建一个新的msg.payload对象,以该格式:

msg.payload =[{
    "series": ["title 1", "title2"...],
    "data": [[{Array of objects}]],
    "labels": ["label1", "label2"...];

填充填充物“数据”值作为我通过硬编码数组的数组很容易:

msg.payload = [{
    "series": ["Curve"],
    "data": [
        [{"x": 10, "y": curveArray[0][1]},
        {"x":  20, "y": curveArray[1][1]},
        {"x":  30, "y": curveArray[2][1]},
        {"x":  40, "y": curveArray[3][1]},
        {"x":  50, "y": curveArray[4][1]},
        {"x":  60, "y": curveArray[5][1]},
        {"x":  70, "y": curveArray[6][1]},
        {"x":  80, "y": curveArray[7][1]},
        {"x":  90, "y": curveArray[8][1]},
        {"x": 100, "y": curveArray[9][1]},
        {"x": 110, "y": curveArray[10][1]},
        {"x": 120, "y": curveArray[11][1]},
        {"x": 130, "y": curveArray[12][1]},
        {"x": 140, "y": curveArray[13][1]},
        {"x": 150, "y": curveArray[14][1]},
        {"x": 160, "y": curveArray[15][1]},
        {"x": 170, "y": curveArray[16][1]},
        {"x": 180, "y": curveArray[17][1]},
        {"x": 190, "y": curveArray[18][1]},
        {"x": 200, "y": curveArray[19][1]},
        {"x": 210, "y": curveArray[20][1]},
        {"x": 220, "y": curveArray[21][1]},
        {"x": 230, "y": curveArray[22][1]},
        {"x": 240, "y": curveArray[23][1]},
        {"x": 250, "y": curveArray[24][1]},
        {"x": 260, "y": curveArray[25][1]},
        {"x": 270, "y": curveArray[26][1]},
        {"x": 280, "y": curveArray[27][1]},
        {"x": 290, "y": curveArray[28][1]},
        {"x": 300, "y": curveArray[29][1]},
        {"x": 310, "y": curveArray[30][1]},
        {"x": 320, "y": curveArray[31][1]},
        {"x": 330, "y": curveArray[32][1]},
        {"x": 340, "y": curveArray[33][1]},
        {"x": 350, "y": curveArray[34][1]},
        {"x": 360, "y": curveArray[35][1]}]],
"labels": ["Curve"]
}]

但是,我需要对9个不同的数据渠道进行此操作,因此这是大量的冗余代码。

我想做的就是

msg.payload = [{
    "series": ["Curve"],
    "data": [for(var j = 0; j < 36; j++)){
           var c = 10;
           [{"x" : c, "y": curveArray[i][1]},
           c = c + 10;
             ]],

循环遍历数组中的每个元素,并在上面的格式中填充“数据”对象属性。

我似乎无法在对象属性内使用VAR。这可以做吗?

I am using node-red to populate a chart using data from PLC. The data comes in as a object, then i convert it to a 2D array 36 items long (the object has 36 elements):

for(var i in pressCurve){
    curveArray.push([i, pressCurve [i]]);

then to get this data onto a chart, I create a new msg.payload object in the format:

msg.payload =[{
    "series": ["title 1", "title2"...],
    "data": [[{Array of objects}]],
    "labels": ["label1", "label2"...];

populating the "data" value as my array of objects via hardcoding is easy:

msg.payload = [{
    "series": ["Curve"],
    "data": [
        [{"x": 10, "y": curveArray[0][1]},
        {"x":  20, "y": curveArray[1][1]},
        {"x":  30, "y": curveArray[2][1]},
        {"x":  40, "y": curveArray[3][1]},
        {"x":  50, "y": curveArray[4][1]},
        {"x":  60, "y": curveArray[5][1]},
        {"x":  70, "y": curveArray[6][1]},
        {"x":  80, "y": curveArray[7][1]},
        {"x":  90, "y": curveArray[8][1]},
        {"x": 100, "y": curveArray[9][1]},
        {"x": 110, "y": curveArray[10][1]},
        {"x": 120, "y": curveArray[11][1]},
        {"x": 130, "y": curveArray[12][1]},
        {"x": 140, "y": curveArray[13][1]},
        {"x": 150, "y": curveArray[14][1]},
        {"x": 160, "y": curveArray[15][1]},
        {"x": 170, "y": curveArray[16][1]},
        {"x": 180, "y": curveArray[17][1]},
        {"x": 190, "y": curveArray[18][1]},
        {"x": 200, "y": curveArray[19][1]},
        {"x": 210, "y": curveArray[20][1]},
        {"x": 220, "y": curveArray[21][1]},
        {"x": 230, "y": curveArray[22][1]},
        {"x": 240, "y": curveArray[23][1]},
        {"x": 250, "y": curveArray[24][1]},
        {"x": 260, "y": curveArray[25][1]},
        {"x": 270, "y": curveArray[26][1]},
        {"x": 280, "y": curveArray[27][1]},
        {"x": 290, "y": curveArray[28][1]},
        {"x": 300, "y": curveArray[29][1]},
        {"x": 310, "y": curveArray[30][1]},
        {"x": 320, "y": curveArray[31][1]},
        {"x": 330, "y": curveArray[32][1]},
        {"x": 340, "y": curveArray[33][1]},
        {"x": 350, "y": curveArray[34][1]},
        {"x": 360, "y": curveArray[35][1]}]],
"labels": ["Curve"]
}]

However, i need to do this for 9 different channels of data, so that is a huge amount of redundant code.

What i'd like to do is something like,

msg.payload = [{
    "series": ["Curve"],
    "data": [for(var j = 0; j < 36; j++)){
           var c = 10;
           [{"x" : c, "y": curveArray[i][1]},
           c = c + 10;
             ]],

to loop through each element in the array, and populate the "data" object property in the format above.

I can't seem to be able to use var inside the object property. Is this possible to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

场罚期间 2025-02-20 00:23:27

您可以尝试一下:

msg.payload =[{
    "series": ["Cruve"],
    "data": pressCurve.map((element, i) => ({ x: i, y: element }) ),
    "labels": ["Curbe Label"]
}];

您可以共享用于填充“系列”和“标签”的数据数组,我认为我的代码可以完成;)

You can try this :

msg.payload =[{
    "series": ["Cruve"],
    "data": pressCurve.map((element, i) => ({ x: i, y: element }) ),
    "labels": ["Curbe Label"]
}];

Can you share the array of data that you use to fill "series" and "labels" I think my code can be completed ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文