从JSON对象Javascript中删除记录

时间:2012-03-14 06:09:34

标签: javascript jquery json

如果我有文件名并想要删除整个记录 例如filename 3.jpg我想删除该文件名的整个记录​​。我怎么做? 更具体的是我想要删除此记录 {         “地位”:“成功”,         “filename”:“3.jpg”,         “mainpic”:“false”,         “fullurl”:“../ userimages / 5c49c449-50b4-43e4-b218-5913b59bd994.jpg”     },

[
{
    "status": "success",
    "filename": "1.jpg",
    "mainpic": "false",
    "fullurl": "../userimages/f0380e08-18de-4fdb-80a2-a912809bda97.jpg"
},
{
    "status": "success",
    "filename": "2.jpg",
    "mainpic": "false",
    "fullurl": "../userimages/7a4362c8-c9c5-48ee-8672-c332eae59f37.jpg"
},
{
    "status": "success",
    "filename": "3.jpg",
    "mainpic": "false",
    "fullurl": "../userimages/5c49c449-50b4-43e4-b218-5913b59bd994.jpg"
},
{
    "status": "success",
    "filename": "eb0b4954-342c-4c3b-87fe-4744511c1b80.jpg",
    "mainpic": "false",
    "fullurl": "../userimages/eb0b4954-342c-4c3b-87fe-4744511c1b80.jpg"
},
{
    "status": "success",
    "filename": "e23ce9e8-5062-41d4-8aea-d11aa795ac45.jpg",
    "mainpic": "true",
    "fullurl": "../userimages/e23ce9e8-5062-41d4-8aea-d11aa795ac45.jpg"
},
{
    "status": "success",
    "filename": "d6343604-e6eb-4caa-82bf-2b5c2b346f71.jpg",
    "mainpic": "false",
    "fullurl": "../userimages/d6343604-e6eb-4caa-82bf-2b5c2b346f71.jpg"
},
{
    "status": "success",
    "filename": "9768f36a-61b8-4f15-995c-28e38dfe48ad.JPG",
    "mainpic": "false",
    "fullurl": "../userimages/9768f36a-61b8-4f15-995c-28e38dfe48ad.JPG"
}

5 个答案:

答案 0 :(得分:4)

尝试此功能:

function deleteRecordByFileName (myArr, fileName) {
    var index = null;
    for (var i =0; i < myArr.length; i++) {
        if (myArr[i].filename === fileName) {
            index = i;
            break;
        }
    }
    if (index !== null) {
        myArr.splice(index, 1);
    }
}

并致电deleteRecordByFileName (yourArray, "3.jpg");

答案 1 :(得分:1)

有一个简单的解决方案..

        var destArr=[];
        var srcArr=[{status:"success",filename:"1.jpg",mainpic:"false",fullurl:"../userimages/f0380e08-18de-4fdb-80a2-a912809bda97.jpg"},{status:"success",filename:"2.jpg",mainpic:"false",fullurl:"../userimages/7a4362c8-c9c5-48ee-8672-c332eae59f37.jpg"},{status:"success",filename:"3.jpg",mainpic:"false",fullurl:"../userimages/5c49c449-50b4-43e4-b218-5913b59bd994.jpg"},{status:"success",filename:"eb0b4954-342c-4c3b-87fe-4744511c1b80.jpg",mainpic:"false",fullurl:"../userimages/eb0b4954-342c-4c3b-87fe-4744511c1b80.jpg"},{status:"success",filename:"e23ce9e8-5062-41d4-8aea-d11aa795ac45.jpg",mainpic:"true",fullurl:"../userimages/e23ce9e8-5062-41d4-8aea-d11aa795ac45.jpg"},{status:"success",filename:"d6343604-e6eb-4caa-82bf-2b5c2b346f71.jpg",mainpic:"false",fullurl:"../userimages/d6343604-e6eb-4caa-82bf-2b5c2b346f71.jpg"},{status:"success",filename:"9768f36a-61b8-4f15-995c-28e38dfe48ad.JPG",mainpic:"false",fullurl:"../userimages/9768f36a-61b8-4f15-995c-28e38dfe48ad.JPG"}];
        for(i in srcArr){ 
          if(srcArr[i].filename!="3.jpg") 
                destArr[i]=srcArr[i];
          }
        // destJson contains the content excluding node with 3.jpg
        //You can replace the original content with destJson
        srcArr=destArr; 
//Now srcArr contains everything excluding with 3.jpg

我做的是,如果文件名不等于'3.jpg',我迭代源数组并将Json节点传递到目标数组。您可以再次使用destArray替换srcArr,以将结果转换为原始JSON。

答案 2 :(得分:0)

这样就可以了。可以使用jQuery更好。

for( var i in demo ) {
    if(demo[i].filename === "3.jpg") {
        delete demo[i];
    }
}

答案 3 :(得分:0)

比Nemoy的回答短

(function (myArr, fileName) {
    for (var i =0; i < myArr.length; i++) {
        if (myArr[i].filename === fileName) {
            myArr.splice(i, 1);
            return;
        }
    }
})(srcArr, "3.jpg");

答案 4 :(得分:0)

好的,我得到了一些好的答案我最终得到的是下面的代码。因为splice选项保持从添加的空节点代替我想要删除的节点。此外,它是我能找到的最干净的选择。这是这里提供的代码的混合,所以我相信每个人都确实提供了很好的信息。

for (var i in picturearr) {
if (picturearr[i].filename === n) {
    picturearr.splice(i, 1); 
}
}