英文:
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= "[{\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;
////// First "For Loop" goes through input data /////
////// and lists transactions ///////////////////////
for(let i=0;i<input.length;i++){
console.log(input[i].transaction);
///////// 2nd "For Loop" is inside the transaction loop. ////////
///////// This goes through transaction data from the above loop /
///////// and lists transactions ///////////////////////////////
for(let j=0;j<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("Transaction ID = "+input[i].transaction+" loop x "+j+": New Total = "+input[i].transactionLines.transactionId);
}
}
<!-- language: lang-html -->
<div>
<h1>
Sample Json Data Shown Below
</h1>
<h2>
Check console for outputs
</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>
<!-- 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 = "[{\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;
////// First "For Loop" goes through input data /////
////// and lists transactions ///////////////////////
for (let i = 0; i < input.length; i++) {
console.log(input[i].transaction);
///////// 2nd "For Loop" is inside the transaction loop. ////////
///////// This goes through transaction data from the above loop /
///////// and lists transactions ///////////////////////////////
for (let j = 0; j < 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("Transaction ID = " + input[i].transaction + " loop x " + j + ": New Total = " + input[i].transactionLines[j].transactionId);
}
}
<!-- language: lang-html -->
<div>
<h1>
Sample Json Data Shown Below
</h1>
<h2>
Check console for outputs
</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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论