82169

function to remove single items or arrays from within an array - javascript

Question:

I have an array that contains some single items and some arrays:

var groceries = ["toothpaste", ["plums", "peaches", "pineapples"], ["carrots", "corn", "green beans"], "orange juice", ["chocolate", "ice cream"], "paper towels", "fish"];

I want to write a function to remove some of these items or arrays when called.

For the single items, I wrote this function:

function removeItems(arr, item) { for ( var i = 0; i < item.length; i++ ) { var index = arr.indexOf(item[i]); if (index > -1) { arr.splice(index, 1); } } };

So then I can call:

removeItems(groceries, ["fish", "orange juice"]);

And this works exactly as I want it to. However, I can't figure out how to make the function also be able to remove the arrays (e.g. ["chocolate", "ice cream"]) from within the larger array. How would I go about that?

Answer1:

Use Array#reduce to create a dictionary of keys. If the item is an array, join all values to create the key. Afterwards filter all items by generating the same keys, and checking them against the dictionary.

Note - Array#filter creates a new array, and doesn't change the original array.

<pre class="snippet-code-js lang-js prettyprint-override">var groceries = ["toothpaste", ["plums", "peaches", "pineapples"], ["carrots", "corn", "green beans"], "orange juice", ["chocolate", "ice cream"], "paper towels", "fish"]; function removeItems(arr, toRemove) { function createKey(item) { return Array.isArray(item) ? item.join('-') : item; } var removeDict = toRemove.reduce(function(d, item) { var key = createKey(item); d[key] = true; return d; }, {}); return arr.filter(function(item) { var key = createKey(item); return !removeDict[key]; }); } var result = removeItems(groceries, ["fish", "orange juice", ["chocolate", "ice cream"]]); console.log(result);

Answer2:

In problems like deleting elements from the subject, i tend to stay away from using the functional array methods since they will iterate according to the initial item count even if you start deleting the items. So my solution would be rather imperative;

<pre class="snippet-code-js lang-js prettyprint-override">function remover(itemToDelete, items){ var i = 0, check = false; for (var item of items){ check = Array.isArray(item) ? item.includes(itemToDelete) : item === itemToDelete; check ? items.splice(i,1) : i++; } return items; } var groceries = ["toothpaste", ["plums", "peaches", "pineapples"], ["carrots", "corn", "green beans"], "orange juice", ["chocolate", "ice cream"], "paper towels", "fish"]; console.log(remover("chocolate", groceries)); console.log(remover("fish", groceries));

Answer3:

This is what I'd do:

<pre class="snippet-code-js lang-js prettyprint-override">const groceries = [ "toothpaste", ["plums", "peaches", "pineapples"], ["carrots", "corn", "green beans"], "orange juice", ["chocolate", "ice cream"], "paper towels", "fish" ]; const equals = (x, y) => JSON.stringify(x) === JSON.stringify(y); const removeItems = (xs, ys) => xs.filter(x => !ys.some(y => equals(x, y))); const result = removeItems(groceries, [ "fish", "orange juice", ["chocolate", "ice cream"] ]); console.log(result);

First, we define what it means for two values to be equal. Next, we define removeItems using this value equality. We say keep all those x which are not equal to any y.

Recommend

  • How to check if a RxJS Observable contains a string in Angular2?
  • Select multiple items in JTextPane
  • Textbox validation in jquery
  • use a single handler for multiple inputs onChange events
  • Runtime error in UVA Online Judge [closed]
  • JSON - slashes not escaping
  • Inversing an interpolation of rotation
  • How does this usort cmp function actually work?
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Is it possible to access block's scope in method?
  • Meteor: Do Something On Email Verification Confirmation
  • Google Custom Search with transparent background
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Why winpcap requires both .lib and .dll to run?
  • retrieve vertices with no linked edge in arangodb
  • How to disable jQuery.jplayer autoplay?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Proper way to use connect-multiparty with express.js?
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How to set the response of a form post action to a iframe source?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • apache spark aggregate function using min value
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?