完全删除 JSON 中的行

Cod*_*ess 2 arrays jquery 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();
}
Run Code Online (Sandbox Code Playgroud)

以及我生成的 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]
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我尝试删除两个中的第二个。

Mr.*_*eks 6

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

\n\n

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

\n\n

delete有关运算符的更多阅读

\n\n
\n

删除数组元素

\n\n

删除数组元素时,数组长度不受影响。即使您删除了数组的最后一个元素,这也成立。

\n
\n\n

例子

\n\n
var arr = [ {name: "Hello"}, {name: "World"} ];\n\n// Delete does not modify an array\'s length, hence the "undefined"\ndelete arr[1]; // => [Object, undefined \xc3\x97 1]\n\n// Splice will change the contents of an array and update its length\narr.splice(1,1); // => [Object]\n
Run Code Online (Sandbox Code Playgroud)\n