Array#delete, but return the array?

remove specific element from array javascript
javascript remove object from array
ruby array delete
ruby remove array from array
ruby remove element from array by index
how to remove an item from array ruby
remove object from json array javascript
ruby array delete all

Is there a built-in method which performs the same function as Array#delete but returns self? I'd like to do it without using a block and clearer than an_ary.-([el]).

I could monkeypatch one, but it seems like a "compact with arguments" method would be a relatively common desire?

If you want to mutate the original array, like delete, here are options:

ary.reject!{|e| e==42 }.something_else
ary.tap{|a| a.delete 42}.something_else
(ary.delete 42;ary).something_else
(ary-=[42]).something_else

If you want a new array to chain from:

ary.reject{|e| e==42 }.something_else
(ary-[42]).something_else

9 Ways To Remove 🗑 Elements From A JavaScript Array [Examples], The pop method removes the last element of the array, returns that element, and updates the length property. The pop method modifies the array� Array#delete() : delete() is a Array class method which returns the array after deleting the mentioned elements. It can also delete a particular element in the array. Syntax: Array.delete() Parameter: obj - specific element to delete Return: last deleted values from the array. Code #1 : Example for delete() method

array.reject{|element| element == value_of_element_to_be_deleted}

Array.prototype.splice(), The splice() method changes the contents of an array by removing or replacing In this case, no element will be deleted but the method will behave as an adding If no elements are removed, an empty array is returned. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

an_ary.-([el]) looks awful.

What about...

an_ary - [el]

?

The most natural way of dealing with mathematical operations is this...

4 - 2

Not this...

4.-(2)

Class: Array (Ruby 2.6.2), How to use length property, delete operator, and pop, shift, and splice The pop method removes the last element of the array, returns that element, and� Array indexing starts at 0, as in C or Java. A negative index is assumed relative to the end of the array --- that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on. Ruby arrays can hold objects such as String, Integer, Fixnum, Hash, Symbol, even other Array objects.

You can do

my_array.first(n) #1

my_array.last(n) #2

If the elements of the array you want to delete, are at the end (1) or at the beginning (2) of the array.

How to Remove Elements from an Array in JavaScript, Delete element at index of array a, returning that element, or nil if index is out of range. a.drop(n). Drop first n elements from array a and return the rest of the� Array#delete(obj) should return obj when there is an object that is equal in the array Added by hasari (Hiro Asari) over 7 years ago. Updated over 7 years ago.

I had this same question for Array#delete_at that returned an array with the element at a specified index removed, which is why I ended up here. Looks like it isn't built in. In case anyone else is interested, I quickly wrote this monkey patch (I gave this virtually no thought regarding efficiency, and I originally didn't handle any cases such as negative indices or out of bounds indices...but then I decided to quick throw a couple in there):

class Array
  def remove_at(i)
    # handle index out of bounds by returning unchanged array
    return self if i >= self.length

    # remove the i-th element from the end if i is negative
    if i < 0
      i += self.length
      # handle index out of bounds by returning unchanged array
      return self if i < 0
    end

    # Return an array composed of the elements before the specified
    # index plus the elements after the specified index
    return self.first(i) + self.last(self.length - i - 1)
  end
end

test = [0,1,2,3,4,5]
puts test.remove_at(3).inspect
puts test.remove_at(5).inspect
puts test.remove_at(6).inspect
puts test.remove_at(-7).inspect
puts test.remove_at(-2).inspect

I had fun whipping this up, so I figured I might as well post it here :)

Ruby Arrays Cheat Sheet, The resulting array is returned from the method, and the array you pass to it remains unchanged. See the following code for an example: var arr = [1, 2, 3,� When you directly give the arguments, this may not be a big problem, but when you have an array array of arbitrary (> 0) length, and do this: [1, 2, 3]. delete (* array) depending on the length of array, you get different types of results. It can be a gotcha. Note that the return value is not much informative in the first place.

Remove Element from an Array in JavaScript, In JavaScript there are a lot of methods in Array itself, but also in other If not, then it returns modified array (array without deleted element)� I would suggest that this feature request be extended to also introduce Array#keep_if. Indeed, Enumerable#reject has two corresponding in-place Array methods: Array#delete_if and Array#reject! I feel that Enumerable#select would benefit from also having two in-place Array methods: Array#keep_if and Array#select!

How to remove an element from Array in JavaScript?, We already know methods that add and remove items from the beginning Here we can see that splice returns the array of removed elements:. Array#delete_if . but that’s essentially an alias for. Array#reject (reject returns nil if the array is unchanged, whereas delete_if returns the unchanged array). So reject just does the same thing as select, but where the block evaluates to false instead of true. What recourse do we have now? It seems that Array has failed us.

Array methods, The delete operator only erases the value of the element and makes the element as undefined. If we want to delete an element and change the length of the array, then How to Get Better at Solving Programming Problems. Hello. 5 days ago, I submitted a pull request on Github which provides a new method for the array objects which is Array#indexes. I have fist edit the Array#index method in order it to return an array of indexes and not a single index (which is the first occurrence it finds).

Comments
  • Possible duplicate of stackoverflow.com/questions/5094283/…
  • Nearly. I'm just wondering if there is a built-in method, really. I'll clarify a bit.
  • ary.delete_if{ |e| e == elem } isn't very clear or concise. Why bother having Array#compact if we can just delete_if{ |e| e.nil? }?
  • @fl00r In that case, use an_ary-=[el]
  • @sawa it's not the method name I dislike, it's requiring the argument to also be an array. It can potentially get confusing when I'm trying to remove an element which is also an array, like ary.-([[1, 2]]). Ideally reject, reject!, delete, and delete_if would be brought further in line with the other destructive and synonymous methods.
  • @gamov Define "overheads" and then you can test and benchmark yourself.
  • reject - Returns a new array containing the items in self for which the given block is not true. +1
  • It looks a bit ugly...but is the most succinct solution, IMO. It allows chaining, and particularly, it allows chaining with &.
  • Succinct? an_ary - [el] is more succint on all scenarios. If you don't want to count the spaces, it has 3 less characters. If you want to count the spaces, it has 1 less character. But I don't prefer this solution for succinctness, I prefer for readability/comprehensibility. an_ary - [el] is clear even for new developers (same syntax as python for example), while an_ary.-([el]) is more complex. It just blurs a trivial operation. The other solution allows chaining, but we are not chaining on the first place. I can quote Occam's Razor: "the simplest solution is almost always the best."
  • Yes, I think you're right, regarding succinctness. My bad.
  • I prefer this solution, but don't know about its performance comparing with other solutions though.
  • Not too keen on this - what happens if someone else changes the array's order at a later date?