0

I want to delete json elements which are satisfying the condition. For that I used the given code

var index = -1;
for (var i = 0; i < data.dashLayout.dashlets.length; i++) {
  if (data.dashLayout.dashlets[i].rowNo == selectedrowno) {
    if (index == -1 || data.dashLayout.dashlets[i].rowNo < data.dashLayout.dashlets[index].rowNo) {
      index = i;
    }
  }
if (index != -1) {
       data.dashLayout.dashlets.splice(index, 1);
    }
}

But iteration is not completing because the data.dashLayout.dashlets.length is reducing with splice. How can I solve this issue? I want to delete all items that are satisfying the condition. Please help

2
  • 3
    Loop backwards. Or adjust i inside the if that does the .splice(). (Also, there's no JSON in your code: JSON is a string format. What you have is objects and an array.) Commented Jul 19, 2014 at 5:24
  • 1
    @nnnnnn looping backwards is definitely the easier option Commented Jul 19, 2014 at 5:25

5 Answers 5

1

Another two solutions

var a = [2,5,8,13,9,1,4,8,10], l = a.length, c = 5;

// using reverse loop
while(l--){
    if(a[l] < c) a.splice(l, 1);
}

// using filter 
var b = a.filter(function(e, c) {
    return --e > c;
});

console.log(a); // [5, 8, 13, 9, 8, 10]  
console.log(b); // [5, 8, 13, 9, 8, 10]
Sign up to request clarification or add additional context in comments.

Comments

0

If you split the array in the loop .the array will be reducing you should not iterate fully , so you have to think different idea . so my solution is you have to store the index based on that you have to remove the array .In the mean time every time the index will be changed so you have to use -1

var index = -1;
var arr = []  // make array
for (var i = 0; i < data.dashLayout.dashlets.length; i++) {
    if (data.dashLayout.dashlets[i].rowNo == selectedrowno) {
        if (index == -1 || data.dashLayout.dashlets[i].rowNo < data.dashLayout.dashlets[index].rowNo) {
            index = i;
        }
    }
    if (index != -1) {
        // data.dashLayout.dashlets.splice(index, 1);

        arr.push(index);  //stored it
    }
}

// to remove 

for(var i in arr){
    if (i == 0) {
        data.dashLayout.dashlets.splice(arr[i], 1); // to remove init
    } else {
        data.dashLayout.dashlets.splice(arr[i] - 1, 1); // now index is changed now use -1
    }
}

Comments

0

I would recommend using the filter method of the Array object. Filter method will return a new array which only contains the elements you want. Please see the definition of the filter method here

The usage will be like the code below

var newDashlets = data.dashLayout.dashlets.filter(function(dashlet, i) {

    // Here your code returns true if you want to keep dashlet. 
    // Return false if you don't want it.

});

Comments

0

in jquery

data.dashLayout.dashlets = $.grep(data.dashLayout.dashlets, function (dashList, indexpos) {
    if (dashList.rowNo == selectedrowno) {
        if (dashList.rowNo < data.dashLayout.dashlets[index].rowNo || index == -1) {
            index = indexpos;
            return false;
        }
    }
});

Comments

0

Using map in jquery

data.dashLayout.dashlets = $.map(data.dashLayout.dashlets, function (dashList, indexpos) {
     if (dashList.rowNo == selectedrowno) {
         if (dashList.rowNo < data.dashLayout.dashlets[index].rowNo || index == -1) {
             index = indexpos;
         } else {
             return dashList;
         }
     }
 });

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.