jquery - 完全删除 JSON 中的行

标签 jquery arrays json

我正在使用一种方法来删除 JSON 中的特定行。用户按下一个按钮,该按钮确定要删除哪一行。我知道正在传入正确的索引,但我的问题是,当我尝试删除该行时,它只是将其替换为 null,所以仍然有一些东西。如何完全删除它?

function removeTest(place) {
    var parsedObject = JSON.parse(localStorage["flexuralStrengthSamples"]);
    delete parsedObject[parseInt(place.data.text)];

    localStorage["flexuralStrengthSamples"] = JSON.stringify(parsedObject);

    console.log(JSON.stringify(parsedObject));

    displaySamples();
}

以及我生成的 JSON:

[{"Result":"Fail","Method":"T97E-v1","Beam1":{"TestingMachineId":"1","BeamAge":"1","WidthUpper":1,"WidthCenter":1,"WidthLower":1,"WidthAverage":1,"DepthRight":1,"DepthCenter":1,"DepthLeft":1,"DepthAverage":1,"MaxLoad":1,"FS":18,"PSI":"18.00000","BreakOutside":"No"},"Beam2":{"BeamAge":"","WidthUpper":null,"WidthCenter":null,"WidthLower":null,"WidthAverage":null,"DepthRight":null,"DepthCenter":null,"DepthLeft":null,"DepthAverage":null,"MaxLoad":null,"FS":null,"PSI":"NaN"},"WaitForCuring":"No","AverageOfBeams":"NaN"},null]

在本例中,我尝试删除两个中的第二个。

最佳答案

根据生成的 JSON,您有一个 Objects {}Array []

为了从 Array 中删除项目(并更新其长度),您必须使用 splice

More reading on the delete operator :

Deleting array elements

When you delete an array element, the array length is not affected. This holds even if you delete the last element of the array.

示例

var arr = [ {name: "Hello"}, {name: "World"} ];

// Delete does not modify an array's length, hence the "undefined"
delete arr[1]; // => [Object, undefined × 1]

// Splice will change the contents of an array and update its length
arr.splice(1,1); // => [Object]

关于jquery - 完全删除 JSON 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33135441/

相关文章:

php - JQuery:自动完成表单提交

java - 骰子统计程序

javascript - Array.prototype.filter 自动过滤掉虚假值

json - Typescript映射Json到接口(interface)解析日期

java - 使用 JSONSerializer.toJava 将 json 转换为 java 对象后始终获取日期字段的值作为当前日期

javascript - 将工具提示/热点添加到 360 图像查看器

javascript - 获取 TR 表外最接近的隐藏值

javascript - 将 float 作为自然数排序

C 指向数组的指针

用于在符号处按行分割 JSON 文本的 Java 例程