-1

assume that I have this array:

var fruits = ["Banana-101", "Orange-105", "Apple-110", "Mango-103", "Banana-102", "Banana-104"];

I need to remove all cells that contain word "Banana" so that the output array be like this:

var fruits = ["Orange-105", "Apple-110", "Mango-103"];

how can I do that by using Jquery?

I tried this but it is not works true:

var found = $.inArray(fruit_id, myarray) > -1; 
myarray.splice(found, 1);

I saw this question and answer,but in mentioned topic, the answer just removes a cell that contains specific word for example "Banana-101" but my question is how can I remove all cells that have for example "Banana"!

Community
  • 1
  • 1
Ali
  • 532
  • 5
  • 17
  • 2
    Possible duplicate of [How to remove specifc value from array using jQuery](http://stackoverflow.com/questions/3596089/how-to-remove-specifc-value-from-array-using-jquery) – Andrew Li May 20 '16 at 12:12
  • @AlexMcMillan var found = $.inArray(fruit_id, myarray) > -1; myarray.splice(found, 1); – Ali May 20 '16 at 12:13
  • In your code, `found` will be either `true` or `false`. Neither is a valid argument for the `index` argument of `splice`. – T.J. Crowder May 20 '16 at 12:15
  • @AndrewL that question remove specific value for example remove "Banana-101", but I need to remove all cells that have "Banana" – Ali May 20 '16 at 12:23

5 Answers5

2

A solution with Array#filter and String#match.

var fruits = ["Banana-101", "Orange-105", "Apple-110", "Mango-103", "Banana-102", "Banana-104"],
    banana = /banana/i; // regex for banana case insensitive

fruits = fruits.filter(function (a) {
    return !a.match(banana);
});

console.log(fruits);
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
2

In your code, found will be either true or false. Neither is a valid argument for the index argument of splice.

If it's important to modify the existing array, loop backward through it and remove entries that match:

var fruits = ["Banana-101", "Orange-105", "Apple-110", "Mango-103", "Banana-102", "Banana-104"];
var fruit_id = "Banana";
console.log("Before: " + fruits);
var index;
for (index = fruits.length - 1; index >= 0; --index) {
  if (fruits[index].indexOf(fruit_id) != -1) {
    fruits.splice(index, 1);
  }
}
console.log("After: " + fruits);

You loop backward so that if you remove an entry, you don't have to worry about whether to update the index or not.

If creating a new, replacement array is an option, use filter:

var fruits = ["Banana-101", "Orange-105", "Apple-110", "Mango-103", "Banana-102", "Banana-104"];
var fruit_id = "Banana";
console.log("Before: " + fruits);
fruits = fruits.filter(function(entry) {
  return entry.indexOf(fruit_id) == -1;
});
console.log("After: " + fruits);

In both cases I've assumed a case-sensitive match. For a case-insensitive one, use toLowerCase on both fruit_id and each entry you're checking.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
1

In ES6 with some trikcy js, it's about 1 line of code.

~ is a bitwise operator that turns -1 falsy and what ever >= 0 truthy

var fruits = ["Banana-101", "Orange-105", "Apple-110", "Mango-103", "Banana-102", "Banana-104"];

var final = fruits.filter(x => ! ~x.indexOf('Banana'));

document.write(final);

Obviously, this snippet works only with ES6 complient webbrowsers ;)

Patrick Ferreira
  • 1,779
  • 1
  • 13
  • 29
0

You can use the filter method to filter out the elements you don't want.

var fruits = [
    "Banana-101",
    "Orange-105",
    "Apple-110",
    "Mango-103", 
    "Banana-102",
    "Banana-104"
];

fruits = $(fruits).filter(function () {
    return this.indexOf("Banana") === -1;
});

console.log(fruits); // ["Orange-105", "Apple-110", "Mango-103"]
Alex McMillan
  • 13,847
  • 6
  • 47
  • 76
0

Keep in mind that your question is not related to jQuery, but the plain javascript. You can take a look over javascript filter method. Don't look for magic method in some frameworks to do this kind of task, take some time and learn javascript. Best regards, and happy learning.

var fruits = ["Banana-101", "Orange-105", "Apple-110", "Mango-103", "Banana-102", "Banana-104"];

var word = "Banana";
fruits = fruits.filter(function(value){
    return value.indexOf(word) == -1;
})
console.log(fruits);