如何调试 JSON.stringify 正在报告的循环引用

How to debug circular reference that JSON.stringify is reporting

我在调用 JSON stringify 时遇到循环引用异常,但我找不到循环引用。似乎 jQuery 是这里的罪魁祸首,但我没有看到问题并且无法进入 JSON stringify。

const list = $('.use-in-reporting-checkbox:checkbox:checked').map(function() 
{
    return this.value;
});

const dataPacket = {
    datasetIDs: list
};

try {
    const real = JSON.stringify(dataPacket);

} catch (error) {
    processError(error);
}

"Error reports: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    property 'selectorData' -> object with constructor 'Object'
    |     property 'elements' -> object with constructor 'Array'
    --- index 0 closes the circle"

But, inspection of dataPacket just shows: "datasetIDs init (37)" with the 
list of checkbox values. Not sure how to debug this.

发生此错误是因为您获得了 jQuery 对象而不是所需的值。

.map-方法 returns jQuery-对象,应通过 get-调用解决:

const ids = $('.use-in-reporting-checkbox:checkbox:checked').map(function() 
{
    return this.id;
}).get();

备选方式:

const ids = jQuery.map($('.use-in-reporting-checkbox:checkbox:checked'), function(v) 
{
    return v.id;
});