admin管理员组

文章数量:1294063

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.

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.

Share asked May 29, 2019 at 17:09 Kate GurmanKate Gurman 731 silver badge9 bronze badges 1
  • arr.splice(0, 1); It changes the array itself and returns removed element(s) as new array. – Alex Kudryashev Commented May 29, 2019 at 17:13
Add a ment  | 

5 Answers 5

Reset to default 7

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)

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);

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);

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))

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

本文标签: javascriptJS delete first element from array by valueStack Overflow