0

I'm having problem with this object array. I need to remove the object who match the removeVal.I't doesn't remove. See attached photos which is highlighted in red. I need to remove that all.

enter image description here

var removeVal = $(this).attr('href');    
         $.each(product_json, function(key, val){

                  if( key == removeVal) {
                        val.splice(key);
                   }

              });

enter image description here

8
  • 1
    make sure removeVal and key's data type should same Commented Nov 11, 2016 at 5:14
  • 1
    show us product_json data Commented Nov 11, 2016 at 5:15
  • you can use filter function . Commented Nov 11, 2016 at 5:17
  • That is the product_json on the picture. Commented Nov 11, 2016 at 5:27
  • what is the removeVal you are passing Commented Nov 11, 2016 at 5:46

3 Answers 3

2

Hope this will helps you if i understand correctly,

var removeVal = $(this).attr('href'); 

delete product_json[removeVal]
Sign up to request clarification or add additional context in comments.

1 Comment

except delete wont really completely delete that object index it just change its value to undefined
1

Probably you meant something like

$.each(product_json, function(key, val){
  if(val == removeVal) {
    product_json.splice(key, 1);
  }
});

But that since splice reindexes, when removing an item the next one won't be checked, but might need to be removed too.

The proper way is using filter:

product_json = product_json.filter(function(val) {
  return val != removeVal;
});

1 Comment

I need to remove the highlighted red. I don't care if it reindex. Still it doesn't work.
0

I think product_json looks like that image you show.

Then,

shouldn't the code be like below?

var removeVal = $(this).attr('href');    
$.each(product_json, function(key, val){
    // val is an array
    for( var i in val ) {
        // val[i] is also an array
        for( var j in val[i] ) {
            // val[i][j] is a value
            if( val[i][j] == removeVal ) {
                val.splice( key );
            }
        }
    }
});

To be more specific,

I need you to explain what the product_json is

and sample of $(this).attr( 'href' )

and why those arrays in that image look like that.

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.