如何将 JSON 数据放入 jQuery/Javascript 数组以备后用?

How can I put JSON data into a jQuery/Javascript array for later use?

我有一些这样的代码:

var data = // coming in from AJAX and confirmed working, don't need to wory about this...

var row = // cloning an existing HTML structure in the DOM

for (i = 0; i < data.length; i++) {
    var rowclone = row.clone();
    var orderLastChangedTime = new Date(data[i].createDate);
    var diffDays = Math.round(Math.abs((currentTime.getTime() - orderLastChangedTime.getTime())/(oneDay)));
    rowclone.find(".home-order-calc").text(diffDays);
    rowclone.find(".home-order-status").text(data[i].status);
    rowclone.find(".home-order-po-number").text(data[i].poNumber);
    rowclone.find(".home-order-number").text(data[i].orderId);
    rowclone.find(".home-order-last-changed").text(orderLastChangedTime);
    rowclone.find(".home-order-lines").text(data[i].itemsCount);
    rowclone.find(".home-order-cost").text(data[i].cost);
    var rowstatus = rowclone.find(".home-order-status").text();
    rowstatus = rowstatus.toUpperCase();
    openJSONitems = [];
    closedJSONitems = [];
    otherJSONitems = [];
    if (status[rowstatus] == "open") {
        openJSONitems.push(rowclone);
    }
    else if (status[rowstatus] == "closed") {
        closedJSONitems.push(rowclone);
    }
    else {
        otherJSONitems.push(rowclone);
    }
    console.log(openJSONitems);
    openJSONitems.appendTo("#home-table-orders");
}

我正在尝试创建 3 个新的 JavaScript 数组,并根据 JSON 负载中的排序标准将数组推入其中。对它们进行排序后,我想保留它们并将它们附加到某些用户操作的 DOM 上……我做错了什么?

您可以将 then 作为数据元素添加到 DOM 对象中。

$('body').data('openItems', openJSONitems);

稍后检索:

var items = $('body').data('openItems');

openJSONitems 是一个数组,它没有 appendTo 方法,您必须遍历该数组并将其元素附加到 "#home-table -命令”。此外,您在每次迭代中都创建了一个新数组。我认为这个改变会解决这个问题。如果你愿意,你也可以避免在 status[rowstatus] == "open" 时直接插入元素的最后一个循环。

var openJSONitems = [],
closedJSONitems = [],
otherJSONitems = [];    
var data = // coming in from AJAX and confirmed working, don't need to wory about this...

var row = // cloning an existing HTML structure in the DOM

for (i = 0; i < data.length; i++) {
    var rowclone = row.clone();
    var orderLastChangedTime = new Date(data[i].createDate);
    var diffDays = Math.round(Math.abs((currentTime.getTime() - orderLastChangedTime.getTime())/(oneDay)));
    rowclone.find(".home-order-calc").text(diffDays);
    rowclone.find(".home-order-status").text(data[i].status);
    rowclone.find(".home-order-po-number").text(data[i].poNumber);
    rowclone.find(".home-order-number").text(data[i].orderId);
    rowclone.find(".home-order-last-changed").text(orderLastChangedTime);
    rowclone.find(".home-order-lines").text(data[i].itemsCount);
    rowclone.find(".home-order-cost").text(data[i].cost);
    var rowstatus = rowclone.find(".home-order-status").text();
    rowstatus = rowstatus.toUpperCase();

    if (status[rowstatus] == "open") {
        openJSONitems.push(rowclone);
    }
    else if (status[rowstatus] == "closed") {
        closedJSONitems.push(rowclone);
    }
    else {
        otherJSONitems.push(rowclone);
    }
}

console.log(openJSONitems);
for (i = 0; i < openJSONitems.length; i++) {
  $(openJSONitems[i]).appendTo("#home-table-orders");
}

您是否考虑过使用 localStorage?

localStorage.setItem('openJSONitems', openJSONitems );

并使用...检索它

var openJSONitems = localStorage.getItem('openJSONitems');