从数组中的子对象中提取数据

huangapple go评论63阅读模式
英文:

Pull data from sub object within array

问题

我看不出我在这里做错了什么,但我在控制台中得到了“undefined”。希望您可以在代码片段中看到,我传递了一些JSON数据。这实际上是2个商店的数据,并且每个商店都有一些交易。我可以取回顶层数据,但我很难从交易行级别取回数据。

每当我尝试引用事务行中的某些内容时,就会出现问题。有人可以指导我如何引用数组的这一部分吗?

非常感谢

    /// 假的JSON数据,已字符串化并放在顶部 ///

    const inputData = "[{\r\n\t\t\"transaction\": 1,\r\n\t\t\"store\": 1,\r\n\t\t\"transactionLines\": [{\r\n\t\t\t\t\"transactionId\": 1,\r\n\t\t\t\t\"lineId\": 3,\r\n\t\t\t\t\"lineValue\": 25\r\n\t\t\t},\r\n\r\n\t\t\t{\r\n\t\t\t\t\"transactionId\": 1,\r\n\t\t\t\t\"lineId\": 2,\r\n\t\t\t\t\"lineValue\": 50\r\n\t\t\t},\r\n\t\t\t{\r\n\t\t\t\t\"transactionId\": 1,\r\n\t\t\t\t\"lineId\": 1,\r\n\t\t\t\t\"lineValue\": 100\r\n\t\t\t}\r\n\t\t]\r\n\t},\r\n\t{\r\n\t\t\"transaction\": 2,\r\n\t\t\"store\": 2,\r\n\t\t\"transactionLines\": [{\r\n\t\t\t\t\"transactionId\": 1,\r\n\t\t\t\t\"lineId\": 2,\r\n\t\t\t\t\"lineValue\": 50\r\n\t\t\t},\r\n\t\t\t{\r\n\t\t\t\t\"transactionId\": 1,\r\n\t\t\t\t\"lineId\": 1,\r\n\t\t\t\t\"lineValue\": 100\r\n\t\t\t}\r\n\t\t]\r\n\t}\r\n]";
    const input = JSON.parse(inputData);

    var total = 0;

    // 第一个“for循环”遍历输入数据并列出交易
    for (let i = 0; i < input.length; i++) {
        console.log(input[i].transaction);

        // 第二个“for循环”在交易循环内部。它遍历上面循环的交易数据并列出交易
        for (let j = 0; j < input[i].transactionLines.length; j++) {

            // 通过将行值添加到现有总值来增加总值
            total = total + input[i].transactionLines[j].lineValue;
            console.log("Transaction ID = " + input[i].transaction + " 循环 x " + j + ": 新总值 = " + total);
        }
    }

希望这能帮助您解决问题。

英文:

I can't see what I'm doing wrong here but I'm getting "undefined" back in my console.
Hopefully you can see in the snippet I am passing in some JSON data. This effectively 2 stores worth of data and against each store there are a few transactions. I can pull back the top level data but I'm struggling to pull back data from the transaction line level.

Its whenever I try to reference something from within my transaction lines. Can anyone point me in the right direction as to how I'm referencing that part of my array?

Many thanks

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

///Dummy JSON Data Stringified and put in the top /////////

const inputData= &quot;[{\r\n\t\t\&quot;transaction\&quot;: 1,\r\n\t\t\&quot;store\&quot;: 1,\r\n\t\t\&quot;transactionLines\&quot;: [{\r\n\t\t\t\t\&quot;transactionId\&quot;: 1,\r\n\t\t\t\t\&quot;lineId\&quot;: 3,\r\n\t\t\t\t\&quot;lineValue\&quot;: 25\r\n\t\t\t},\r\n\r\n\t\t\t{\r\n\t\t\t\t\&quot;transactionId\&quot;: 1,\r\n\t\t\t\t\&quot;lineId\&quot;: 2,\r\n\t\t\t\t\&quot;lineValue\&quot;: 50\r\n\t\t\t},\r\n\t\t\t{\r\n\t\t\t\t\&quot;transactionId\&quot;: 1,\r\n\t\t\t\t\&quot;lineId\&quot;: 1,\r\n\t\t\t\t\&quot;lineValue\&quot;: 100\r\n\t\t\t}\r\n\t\t]\r\n\t},\r\n\t{\r\n\t\t\&quot;transaction\&quot;: 2,\r\n\t\t\&quot;store\&quot;: 2,\r\n\t\t\&quot;transactionLines\&quot;: [{\r\n\t\t\t\t\&quot;transactionId\&quot;: 1,\r\n\t\t\t\t\&quot;lineId\&quot;: 2,\r\n\t\t\t\t\&quot;lineValue\&quot;: 50\r\n\t\t\t},\r\n\t\t\t{\r\n\t\t\t\t\&quot;transactionId\&quot;: 1,\r\n\t\t\t\t\&quot;lineId\&quot;: 1,\r\n\t\t\t\t\&quot;lineValue\&quot;: 100\r\n\t\t\t}\r\n\t\t]\r\n\t}\r\n]&quot;


const input = JSON.parse(inputData);

var  total = 0;

//////   First &quot;For Loop&quot; goes through input data  /////
//////   and lists transactions  ///////////////////////

for(let i=0;i&lt;input.length;i++){
console.log(input[i].transaction);

/////////   2nd &quot;For Loop&quot; is inside the transaction loop.  ////////
/////////   This goes through transaction data from the above loop /
/////////    and lists transactions  ///////////////////////////////

    for(let j=0;j&lt;input[i].transactionLines.length;j++){

/////////   Increase the total by adding the line value to the /////
/////////   existing total  ////////////////////////////////////////

total = total+input[i].lineValue
console.log(input[i].transactionLines.lineId);
  console.log(&quot;Transaction ID = &quot;+input[i].transaction+&quot; loop x &quot;+j+&quot;: New Total = &quot;+input[i].transactionLines.transactionId);
    }
}

<!-- language: lang-html -->

&lt;div&gt;
&lt;h1&gt;
Sample Json Data Shown Below
&lt;/h1&gt;
&lt;h2&gt;
Check console for outputs
&lt;/h2&gt;
&lt;/div&gt;

&lt;xmp&gt;
  
  [{
    &quot;transaction&quot;: 1,
    &quot;store&quot;: 1,
    &quot;transactionLines&quot;: [{
            &quot;transactionId&quot;: 1,
            &quot;lineId&quot;: 3,
            &quot;lineValue&quot;: 25
        },

        {
            &quot;transactionId&quot;: 1,
            &quot;lineId&quot;: 2,
            &quot;lineValue&quot;: 50
        },
        {
            &quot;transactionId&quot;: 1,
            &quot;lineId&quot;: 1,
            &quot;lineValue&quot;: 100
        }
    ]
},
{
    &quot;transaction&quot;: 2,
    &quot;store&quot;: 2,
    &quot;transactionLines&quot;: [{
            &quot;transactionId&quot;: 1,
            &quot;lineId&quot;: 2,
            &quot;lineValue&quot;: 50
        },
        {
            &quot;transactionId&quot;: 1,
            &quot;lineId&quot;: 1,
            &quot;lineValue&quot;: 100
        }
    ]
}
]
&lt;/xmp&gt;

<!-- end snippet -->

答案1

得分: 1

你忘记使用 j 变量。

///虚拟 JSON 数据已被字符串化并放在顶部//////

const inputData = "[{\r\n\t\t\"transaction\": 1,\r\n\t\t\"store\": 1,\r\n\t\t\"transactionLines\": [{\r\n\t\t\t\t\"transactionId\": 1,\r\n\t\t\t\t\"lineId\": 3,\r\n\t\t\t\t\"lineValue\": 25\r\n\t\t\t},\r\n\r\n\t\t\t{\r\n\t\t\t\t\"transactionId\": 1,\r\n\t\t\t\t\"lineId\": 2,\r\n\t\t\t\t\"lineValue\": 50\r\n\t\t\t},\r\n\t\t\t{\"transactionId\": 1,\r\n\t\t\t\t\"lineId\": 1,\r\n\t\t\t\t\"lineValue\": 100\r\n\t\t\t}\r\n\t\t]\r\n\t},\r\n\t{\r\n\t\t\"transaction\": 2,\r\n\t\t\"store\": 2,\r\n\t\t\"transactionLines\": [{\r\n\t\t\t\t\"transactionId\": 1,\r\n\t\t\t\t\"lineId\": 2,\r\n\t\t\t\t\"lineValue\": 50\r\n\t\t\t},\r\n\t\t\t{\"transactionId\": 1,\r\n\t\t\t\t\"lineId\": 1,\r\n\t\t\t\t\"lineValue\": 100\r\n\t\t\t}\r\n\t\t]\r\n\t}\r\n]";

const input = JSON.parse(inputData);

var total = 0;

//////   第一个 “For Loop” 遍历输入数据  /////
//////   并列出交易  ///////////////////////

for (let i = 0; i < input.length; i++) {
  console.log(input[i].transaction);

  /////////   第二个 “For Loop” 在交易循环内部。 ////////
  /////////   这遍历了上面循环中的交易数据 /
  /////////   并列出交易  ///////////////////////////////

  for (let j = 0; j < input[i].transactionLines.length; j++) {

    /////////   通过将行值添加到现有总和来增加总数  ////////////////////////////////////////

    total = total + input[i].transactionLines[j].lineValue
    console.log(input[i].transactionLines[j].lineId);
    console.log("Transaction ID = " + input[i].transaction + " loop x " + j + ": New Total = " + input[i].transactionLines[j].transactionId);
  }
}
<div>
  <h1>
    以下是示例的 Json 数据
  </h1>
  <h2>
    请查看控制台输出
  </h2>
</div>

<xmp>
[{ "transaction": 1, "store": 1, "transactionLines": [{ "transactionId": 1, "lineId": 3, "lineValue": 25 }, { "transactionId": 1, "lineId": 2, "lineValue": 50 }, { "transactionId": 1, "lineId": 1, "lineValue": 100 } ] }, { "transaction": 2, "store": 2,
"transactionLines": [{ "transactionId": 1, "lineId": 2, "lineValue": 50 }, { "transactionId": 1, "lineId": 1, "lineValue": 100 } ] } ]
</xmp>
英文:

You forgot to use j variable

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

///Dummy JSON Data Stringified and put in the top /////////

const inputData = &quot;[{\r\n\t\t\&quot;transaction\&quot;: 1,\r\n\t\t\&quot;store\&quot;: 1,\r\n\t\t\&quot;transactionLines\&quot;: [{\r\n\t\t\t\t\&quot;transactionId\&quot;: 1,\r\n\t\t\t\t\&quot;lineId\&quot;: 3,\r\n\t\t\t\t\&quot;lineValue\&quot;: 25\r\n\t\t\t},\r\n\r\n\t\t\t{\r\n\t\t\t\t\&quot;transactionId\&quot;: 1,\r\n\t\t\t\t\&quot;lineId\&quot;: 2,\r\n\t\t\t\t\&quot;lineValue\&quot;: 50\r\n\t\t\t},\r\n\t\t\t{\r\n\t\t\t\t\&quot;transactionId\&quot;: 1,\r\n\t\t\t\t\&quot;lineId\&quot;: 1,\r\n\t\t\t\t\&quot;lineValue\&quot;: 100\r\n\t\t\t}\r\n\t\t]\r\n\t},\r\n\t{\r\n\t\t\&quot;transaction\&quot;: 2,\r\n\t\t\&quot;store\&quot;: 2,\r\n\t\t\&quot;transactionLines\&quot;: [{\r\n\t\t\t\t\&quot;transactionId\&quot;: 1,\r\n\t\t\t\t\&quot;lineId\&quot;: 2,\r\n\t\t\t\t\&quot;lineValue\&quot;: 50\r\n\t\t\t},\r\n\t\t\t{\r\n\t\t\t\t\&quot;transactionId\&quot;: 1,\r\n\t\t\t\t\&quot;lineId\&quot;: 1,\r\n\t\t\t\t\&quot;lineValue\&quot;: 100\r\n\t\t\t}\r\n\t\t]\r\n\t}\r\n]&quot;


const input = JSON.parse(inputData);

var total = 0;

//////   First &quot;For Loop&quot; goes through input data  /////
//////   and lists transactions  ///////////////////////

for (let i = 0; i &lt; input.length; i++) {
  console.log(input[i].transaction);

  /////////   2nd &quot;For Loop&quot; is inside the transaction loop.  ////////
  /////////   This goes through transaction data from the above loop /
  /////////    and lists transactions  ///////////////////////////////

  for (let j = 0; j &lt; input[i].transactionLines.length; j++) {

    /////////   Increase the total by adding the line value to the /////
    /////////   existing total  ////////////////////////////////////////

    total = total + input[i].lineValue
    console.log(input[i].transactionLines[j].lineId);
    console.log(&quot;Transaction ID = &quot; + input[i].transaction + &quot; loop x &quot; + j + &quot;: New Total = &quot; + input[i].transactionLines[j].transactionId);
  }
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;h1&gt;
    Sample Json Data Shown Below
  &lt;/h1&gt;
  &lt;h2&gt;
    Check console for outputs
  &lt;/h2&gt;
&lt;/div&gt;

&lt;xmp&gt;

  [{ &quot;transaction&quot;: 1, &quot;store&quot;: 1, &quot;transactionLines&quot;: [{ &quot;transactionId&quot;: 1, &quot;lineId&quot;: 3, &quot;lineValue&quot;: 25 }, { &quot;transactionId&quot;: 1, &quot;lineId&quot;: 2, &quot;lineValue&quot;: 50 }, { &quot;transactionId&quot;: 1, &quot;lineId&quot;: 1, &quot;lineValue&quot;: 100 } ] }, { &quot;transaction&quot;: 2, &quot;store&quot;: 2,
  &quot;transactionLines&quot;: [{ &quot;transactionId&quot;: 1, &quot;lineId&quot;: 2, &quot;lineValue&quot;: 50 }, { &quot;transactionId&quot;: 1, &quot;lineId&quot;: 1, &quot;lineValue&quot;: 100 } ] } ]
&lt;/xmp&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 18:43:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定