3

I have an array:

arr = [50, 40, 50, 50];

I need to delete first element, that equal 50 and dont touch another. This code return only [40].

arr = arr.filter(function(e) {return e !== 50}) // [40]

But I need

arr = arr.somefunction(function(e) {return e !== 50}) // [40, 50, 50]

I would be grateful for any help.

1
  • arr.splice(0, 1); It changes the array itself and returns removed element(s) as new array. Commented May 29, 2019 at 17:13

5 Answers 5

8

You can use findIndex and splice()

let arr = [50, 40, 50, 50];
arr.splice(arr.findIndex(a => a === 50), 1);
console.log(arr)

If you need it on the prototype of the Array then you can define your custom method.

function removeFirst(cb){
  for(let i = 0;i<this.length;i++){
    if(cb(this[i],i,this)){
      return this.slice(0,i).concat(this.slice(i+1));
    }
  }
  return this;
}

Object.defineProperty(Array.prototype,'removeFirst',{
  value:removeFirst
})

let arr = [50,40,50,50];
let res = arr.removeFirst(x => x === 50);
console.log(res)

Sign up to request clarification or add additional context in comments.

2 Comments

@KateGurman Consider accepting the answer if it worked for you.
Attention: if item not exists in array it removes the first item! consider check findIndex result to not be -1 and then use splice.
2

You could take a flag and change it of the first value is found.

var array = [40, 40, 50, 40, 50, 40, 50],
    found = false;

array = array.filter(v => found || !(found = v === 50));

console.log(array);

With a counter, you could specify the amount of value for filtering out.

var array = [40, 40, 50, 40, 50, 40, 50],
    count = 1;

array = array.filter(v => !count || (count -= v === 50));

console.log(array);

Comments

2

findIndex() returns the index of the first occurrence of the function provided. After that, you can just delete the element using splice().

let arr = [50, 40, 50, 50];
arr.splice(arr.findIndex(function(e) {return e === 50}), 1);
console.log(arr);

Comments

0

You can use a variable to keep track whether you deleted any value or not

let deleteFirst = (value) => {
  let arr = [50, 40, 50, 50];
  let del = false
  return arr.filter(val=> {
    if(val === value && !del){
      del = true
      return false
    }
    return true
  })
}

console.log(deleteFirst(50))
console.log(deleteFirst(40))

Comments

0

Here's a curried function for that. It does not modify your original array, returning a new one instead:

const removeFirst = (val) => ( xs = [], idx = xs .indexOf (val) ) => idx > -1
  ? xs .slice (0, idx) .concat ( xs .slice (idx + 1) )
  : xs

console .log (
  removeFirst (50) ([50, 40, 50, 50])
)

If you prefer to call it like remove(50, [50, 40, 50, 50]), then you can just change the first line:

const removeFirst = (val, xs = [], idx = xs .indexOf (val) ) => idx > -1
  ? xs .slice (0, idx) .concat ( xs .slice (idx + 1) )
  : xs

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.