简体   繁体   English

如何从 JavaScript(HTML) 中的 JSON 对象获取没有键的值

[英]How to get values without key from JSON object in JavaScript(HTML)

 [17/02/2020] [18/02/2020] [19/02/2020] itemseq 1 Allocated 2 6 7 itemname 2 Out Of Order 0 0 0

 success: function (result) { debugger; var Item1 = JSON.parse(result.Item1) if (Item1.length == 0) { $('<tr><td colspan="4">No Record Found</td></tr>').appendTo($('#Tablev')); } $('#Tablev tr:not(:first)').empty(); $('<tr>' + '<th ' + item.itemseq+ '</th>' + '<th>' + item.itemname + '</th>' + (need to get the Date and date values like we got the above 2 things) '</tr>').appendTo($('#Tablev')); });

I'm trying to add key values for eg-(dates "16/02/2020"): and the value it contains(0) and show them on a grid, I managed to get the itemseq and itemname but I have no idea how to get the other two key values.我正在尝试为 eg-(dates "16/02/2020"):添加键值:以及它包含的值 (0)并将它们显示在网格上,我设法获得了itemseqitemname但我不知道如何获取其他两个键值。 The dates and value are received from mssql on user input date ranges.日期和值是从用户输入日期范围内的 mssql 接收的。

JSON string in JavaScript I receive is (the json string keeps ongoing on this is half of the code)我收到的 JavaScript 中的 JSON 字符串是(json 字符串一直在运行,这是代码的一半)

[
  {
    "itemseq": 1,
    "itemname": "Allocated",
    "17/02/2020": 2,
    "18/02/2020": 6,
    "19/02/2020": 7,
    "20/02/2020": 3,
    "21/02/2020": 7,
    "22/02/2020": 6,
    "23/02/2020": 2,
    "24/02/2020": 5
  },
  {
    "itemseq": 2,
    "itemname": "Out Of Order",
    "17/02/2020": 0,
    "18/02/2020": 0,
    "19/02/2020": 0,
    "20/02/2020": 0,
    "21/02/2020": 0,
    "22/02/2020": 0,
    "23/02/2020": 0,
    "24/02/2020": 0
  },
  {
    "itemseq": 3,
    "itemname": "Daily Occupancy %",
    "17/02/2020": 28.57,
    "18/02/2020": 85.71,
    "19/02/2020": 100,
    "20/02/2020": 42.85,
    "21/02/2020": 100,
    "22/02/2020": 85.71,
    "23/02/2020": 28.57,
    "24/02/2020": 71.42
  }
]  

How could i take the dates and the values of the dates it contains "(date)18/02/2020": (value)85.71,我怎么能取它包含“(日期)18/02/2020”的日期和日期的值:(值)85.71,

when we are using the string we can break the string and get the values like (item.itemseq item.itemname) and i want to know how could i get the values of dates and the date values because these data are passed to a grid and gonna be used there..当我们使用字符串时,我们可以打破字符串并获得像 (item.itemseq item.itemname) 这样的值,我想知道如何获得日期值和日期值,因为这些数据被传递到网格和会在那里使用..
eg- columns- the dates [18/02/2020] [19/02/2020] and the rows wise we have the [Allocated][Out Of Order] for the specific date it should contains the value that date has [85.71][100]例如-列-日期 [18/02/2020] [19/02/2020] 和行明智我们有 [Allocated][Out Of Order] 特定日期它应该包含日期的值 [85.71] [100]

Try this尝试这个

const json = [{"itemseq":1,"itemname":"Allocated","17/02/2020":2.0000,"18/02/2020":6.0000,"19/02/2020":7.0000,"20/02/2020":3.0000,"21/02/2020":7.0000,"22/02/2020":6.0000,"23/02/2020":2.0000,"24/02/2020":5.0000},{"itemseq":2,"itemname":"Out Of Order","17/02/2020":0.0000,"18/02/2020":0.0000,"19/02/2020":0.0000,"20/02/2020":0.0000,"21/02/2020":0.0000,"22/02/2020":0.0000,"23/02/2020":0.0000,"24/02/2020":0.0000},{"itemseq":3,"itemname":"Daily Occupancy %","17/02/2020":28.5700,"18/02/2020":85.7100,"19/02/2020":100.0000,"20/02/2020":42.8500,"21/02/2020":100.0000,"22/02/2020":85.7100,"23/02/2020":28.5700,"24/02/2020":71.4200}]

json.map((item) => {
 console.log(item["21/02/2020"])
})

It will return:它将返回:

7
0
100

If you mean add a value to each item you can try this如果您的意思是为每个项目添加一个值,您可以试试这个

 let data = [ {"itemseq":1,"itemname":"Allocated","17/02/2020":2.0000,"18/02/2020":6.0000,"19/02/2020":7.0000,"20/02/2020":3.0000,"21/02/2020":7.0000,"22/02/2020":6.0000,"23/02/2020":2.0000,"24/02/2020":5.0000}, {"itemseq":2,"itemname":"Out Of Order","17/02/2020":0.0000,"18/02/2020":0.0000,"19/02/2020":0.0000,"20/02/2020":0.0000,"21/02/2020":0.0000,"22/02/2020":0.0000,"23/02/2020":0.0000,"24/02/2020":0.0000}, {"itemseq":3,"itemname":"Daily Occupancy %","17/02/2020":28.5700,"18/02/2020":85.7100,"19/02/2020":100.0000,"20/02/2020":42.8500,"21/02/2020":100.0000,"22/02/2020":85.7100,"23/02/2020":28.5700,"24/02/2020":71.4200} ]; let copy = []; data.forEach((item) => { item["16/02/2020"] = 0; copy.push(item); }); console.log(copy);

"16/02/2020" index is added with value : 0 to each item "16/02/2020" 索引添加了 value : 0 到每个项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM