0

If I have the filename and want to delete an entire record for example filename 3.jpg I want to delete the entire record for that filename. how do I do that? a bit more specifics is I want to remove this record { "status": "success", "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"
}

]

1
  • If possible, it's better if you stop it from coming into your json result, when you requset the files. You dont use as much bandwidth and you dont have change your json object :-) Commented Mar 14, 2012 at 6:14

5 Answers 5

4

Try this function:

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);
    }
}

and call deleteRecordByFileName (yourArray, "3.jpg");

Sign up to request clarification or add additional context in comments.

Comments

1

There is one simple solution for this..

        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

What I did is, I iterated the source array and passed Json nodes into destination array if file name is not equal to '3.jpg'. You can replace srcArr by destArray again to have result into original JSON.

2 Comments

with a bit of modification I did get this one to work. it had the same issue as westo's providing a null record.
var p = 0; for (i in picturearr) { if (picturearr[i].filename != n) { destArr[p] = picturearr[i]; p++; } }
1

shorter than Nemoy's answer

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

1 Comment

I am afraid to say I am not sure how to even impliment this one
0

This will do the trick. Could be nicer using jQuery.

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

2 Comments

Ok So I tried this one which I do like the simplicity of this one but I found that although this does delete the record in question it does not shift the remaining records over to fill the place so you end up with a null record.
var p = 0; for (i in picturearr) { if (picturearr[i].filename != n) { destArr[p] = picturearr[i]; p++; alert('picture array ' + picturearr[i].filename + ' N value ' + n + ' position ' + i); } }
0

Ok I got a few good answers What I did end up going with is the below code. as the splice option keeps from a null node being added in place of the node I want to remove. Plus it is the cleanest option I could find. This is a mixture of the codes that were provided here so I believe everyone really did provide good information.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.