3

I have an array that contains any number of subarrays, each containing exactly two values.

i.e: interestArray[[1, 5], [3, 8] ... ]

How do I remove say the subarray containing the values [3, 8]?

My code is:

$('td', container).click(function(){
      if(!$(this).hasClass('purchased') && !$(this).hasClass('manu'))
      {
        var manuId = $(this).parent().children('td:first-child').data('manu-id');
        var typeId = $(this).data('type-id');
        if($(this).hasClass('interest'))
        {
          $(this).removeClass('interest');
          $(this).parent().children('td.manu').removeClass('interest');
          var index = interestArray.indexOf([manuId, typeId]);
          interestArray.splice(index, 1);
        } else {
          $(this).addClass('interest');
          $(this).parent().children('td.manu').addClass('interest');

          interestArray.push([manuId, typeId]);
        }
      //updateSurvey(interestsArray);
      console.log(interestArray)
      }
    })

The below section does not work, and simply removes the first subarray.

var index = interestArray.indexOf([manuId, typeId]);
interestArray.splice(index, 1);
3
  • possible duplicate of Comparing two arrays in Javascript Commented Jul 15, 2013 at 9:20
  • Comparing is not filtering/removing elements. Commented Jul 15, 2013 at 11:38
  • No, but your main problem is identifying the item you want to remove. Once you've found it, removing it becomes trivial. Commented Jul 15, 2013 at 12:53

3 Answers 3

2

Here's a generic approach with your requirements:

var arr = [[1,2],[3,4],[5,6]];
var remove = [3,4];

for (var i=0; i<arr.length; i++) {
  if (arr[i][0] == remove[0] && arr[i][1] == remove[1]) {
    arr.splice(i, 1);
    break;
  }
}

console.log(arr); //=> [[1,2],[5,6]]
Sign up to request clarification or add additional context in comments.

2 Comments

You might want to break; when you find a match, otherwise loop backwards, in case there's a possibility of multiple matches
This will skip the second of consecutive matches. (So if you start with var arr = [[1,2],[3,4],[3,4],[5,6]];, the output at the end will be [[1,2],[3,4],[5,6]] (with the second [3,4] still in the list).
1

For a general approach, you can filter the array:

var reducedArray = interestArray.filter(function (item) {
    return item[0] != manuId || item[1] != typeId;
});

You cannot use indexOf because that looks for the identical object (not merely an equivalent one).

If you're running an earlier version of JS that doesn't have Array.filter, there's a nice shim on the filter doc page linked to above.

Comments

0

Here is my personal solution more complete to avoid multiple entry issue and the break; thing seen above, it also avoids an issue if the array is after entry removal (it is jquery based but you can make a regular loop if you feel more comfy with it):

    $.each( answers, function( index, value ){
        if (typeof answers[index] != "undefined") 
        {
            if(answers[index]["question_id"]==answer_to_del)
            {
                delete answers[index];
            }
        }

    });
    //Clean answer array from empty values created above
    answers = answers.filter(function(n){ return n != undefined }); 

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.