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
5 Answers
Reset to default 7You 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
版权声明:本文标题:javascript - JS: delete first element from array by value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741589193a2387019.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论