1

I need to remove one element from javascript array. The element I want to remove is the value of 'NT'. I have a HTML input

        <input type="text" id="caseType" size="50"/>

We populate it with

var caseTypeJson = jQuery.parseJSON('${crFilingCaseDetailForm.caseTypes}');

I want to remove one element from the javascript array

  jQuery(function () {

        jQuery.each(caseTypeJson, function (index, item) {
            if(("NT") == item.value){  // remove this element
                caseTypeJson.splice(index,1);
            }
            if (item.value == '${crFilingCaseDetailForm.selectedCase.caseType}') {
                jQuery('#caseType').val(item.value + ' - ' + item.description);
                jQuery('#selectedCaseType').val(item.value);
            }
        });

   });

This splice approach is not working. In doing some prior research I also tried the javascript delete too and that left the undefined element. Does this seem like a good way to do this?

Thanks,

Tom

1
  • This question isn't clear. What does the HTML input control have to do with removing an element from an array? Commented Nov 20, 2013 at 22:23

1 Answer 1

2

You could try using grep.

 var values = jQuery.grep(caseTypeJson, function(item) {
     if (("NT") != item.value) return item;
 });

This will give you an array without the NT value.

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

1 Comment

Thanks @vilecoder! It worked great. Sorry @Charlie Kilian I just through that in as a bonus... :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.