如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
ES6且无突变:(2016年10月)
const removeByIndex = (list, index) => [ ...list.slice(0, index), ...list.slice(index + 1) ]; output = removeByIndex([33,22,11,44],1) //=> [33,11,44] console.log(output)
其他回答
根据所有主要正确的答复并考虑到建议的最佳做法(特别是不直接使用Array.prototype),我提出了以下代码:
function arrayWithout(arr, values) {
var isArray = function(canBeArray) {
if (Array.isArray) {
return Array.isArray(canBeArray);
}
return Object.prototype.toString.call(canBeArray) === '[object Array]';
};
var excludedValues = (isArray(values)) ? values : [].slice.call(arguments, 1);
var arrCopy = arr.slice(0);
for (var i = arrCopy.length - 1; i >= 0; i--) {
if (excludedValues.indexOf(arrCopy[i]) > -1) {
arrCopy.splice(i, 1);
}
}
return arrCopy;
}
在审查上述功能时,尽管运作良好,但我意识到业绩可能有所改进。 使用ES6而不是ES5是一种更好的方法。
const arrayWithoutFastest = (() => {
const isArray = canBeArray => ('isArray' in Array)
? Array.isArray(canBeArray)
: Object.prototype.toString.call(canBeArray) === '[object Array]';
let mapIncludes = (map, key) => map.has(key);
let objectIncludes = (obj, key) => key in obj;
let includes;
function arrayWithoutFastest(arr, ...thisArgs) {
let withoutValues = isArray(thisArgs[0]) ? thisArgs[0] : thisArgs;
if (typeof Map !== 'undefined') {
withoutValues = withoutValues.reduce((map, value) => map.set(value, value), new Map());
includes = mapIncludes;
} else {
withoutValues = withoutValues.reduce((map, value) => { map[value] = value; return map; } , {});
includes = objectIncludes;
}
const arrCopy = [];
const length = arr.length;
for (let i = 0; i < length; i++) {
// If value is not in exclude list
if (!includes(withoutValues, arr[i])) {
arrCopy.push(arr[i]);
}
}
return arrCopy;
}
return arrayWithoutFastest;
})();
如何使用 :
const arr = [1,2,3,4,5,"name", false];
arrayWithoutFastest(arr, 1); // will return array [2,3,4,5,"name", false]
arrayWithoutFastest(arr, 'name'); // will return [2,3,4,5, false]
arrayWithoutFastest(arr, false); // will return [2,3,4,5]
arrayWithoutFastest(arr,[1,2]); // will return [3,4,5,"name", false];
arrayWithoutFastest(arr, {bar: "foo"}); // will return the same array (new copy)
我目前正在写博客文章, 我已将数个阵列解决方案设定为基准, 没有问题, 并比较运行时间 。 一旦我完成该文章, 我将更新此答案, 并使用链接 。 只需告知您, 我将以上内容与没有浏览器支持地图的 Lodash 比较, 以防浏览器支持地图, 它比 Lodash 还要快! 注意我没有使用 Array. prototype. indexof 或 Array. prototype 。 包含在地图或天体中将ExcludeValue 包装成快的查询 !
您可以从数组中添加一个原型函数来“ 移除” 元素 。
以下示例显示当我们知道一个元素的索引时, 如何简单地从数组中删除一个元素。 我们用它来使用 Array. filter 方法 。
Array. prototype. removeByIndex = 函数 (i) {如果 (! Number. is Integer(i) {i < 0) {/ i 必须是一个整数返回它;} 返回此. filter( f, indx) { indx! = i)} var a = [5, - 89, 2 * 2, " some string", null, froid, 未定义, 20, null, 5]; var b = a. remove ByIndex(2); control.log(a); control.log(b);
有时候我们不知道元素的索引
Array. prototype. remove = 函数 (i) { 返回此. filter (f \ \ f \ f y = i) {var a = [5, - 89, 2 ( 2 * 2), “ 一些字符串 ” 无效, 错误, 未定义, 20, 无效, 5] ; var b = a. remove(5). remove (null); 控制台. log (a); 控制台. log (b) / 它消除了所有搜索值的发生次数 。
但是,当我们只想要删除搜索值的首次出现时, 我们可以在函数中使用 Array. indexof 方法 。
Array. prototype. removeFirst = 函数 (i) {i = this.indexof (i); if (! Number. is Integer (i) {i < 0) {返回此 ;} 返回此. filter (f, indx) { indx = indx ! = i)} var a = [5, - 89, 2 * 2, " some string", null, fraud, 未定义, 20, null, 5]; var b = a.remove First(5).remove First(nell); translavor.log(a) ; controad.log(b);
如果您想要 [.].remove(el) 类似语法的语法, 和其他编程语言一样, 那么您可以添加此代码 :
// Add remove method to Array prototype
Array.prototype.remove = function(value, count=this.length) {
while(count > 0 && this.includes(value)) {
this.splice(this.indexOf(value), 1);
count--;
}
return this;
}
用法
// Original array
const arr = [1,2,2,3,2,5,6,7];
// Remove all 2s from array
arr.remove(2); // [1,3,5,6,7]
// Remove one 2 from beginning of array
arr.remove(2, 1); // [1,2,3,2,5,6,7]
// Remove two 2s from beginning of array
arr.remove(2, 2); // [1,3,2,5,6,7]
您可以根据您的需求操控该方法 。
function array_remove(arr, index) {
for (let i = index; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
arr.length -= 1;
return arr;
}
my_arr = ['A', 'B', 'C', 'D'];
console.log(array_remove(my_arr, 0));
只将前34岁从年龄上移走,不是所有34岁的人:
ages.splice(ages.indexOf(34), 1);
或者您可以在全球定义一种方法 :
function remove(array, item){
let ind = array.indexOf(item);
if(ind !== -1)
array.splice(ind, 1);
}
将所有34岁的人除去:
ages = ages.filter(a => a !== 34);