简体   繁体   English

从数组中删除值

[英]Remove value from array

I am trying to figure out how if you are adding values to an array and then the same value comes up you have push it to the same array to not push it up and remove the duplicate of the value from the array. 我试图弄清楚如果您要向数组中添加值,然后出现相同的值,则将其推入同一数组以不将其推入并从数组中删除值的重复项。

This is my code below: 这是我的代码如下:

if (!_.includes(scope.index, val)) {
    scope.index.push(val);
} else {
    _.remove(scope.index, val);
   console.log(scope.index);
}

您必须使用谓词函数调用_.remove() ,该函数检查当前元素是否等于val

_.remove(arr, x=> x === val)

It is as simple as this in JS 在JS中就这么简单

if ( scope.index.indexOf( val ) != -1 )
{
  scope.index.push( val );
}

To remove the pushed value 删除推送的值

var index = scope.index.indexOf( val );
scope.index.splice(index,1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM