Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
This question isn't clear. What does the HTML input control have to do with removing an element from an array? –  Charlie Kilian Nov 20 '13 at 22:23

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks @vilecoder! It worked great. Sorry @Charlie Kilian I just through that in as a bonus... :) –  Zirous Tom Nov 21 '13 at 14:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.