[英]JavaScript - more efficient way to truncate an array
I have an int array sorted ascending and I would like to remove array items from the tail according to the specific boundary. 我有一个按升序排序的int数组,我想根据特定的边界从尾部删除数组项。 What is a better way to truncate an array in my case? 在我的情况下截断数组的更好方法是什么? Consider next code snippets: 考虑下一段代码片段:
var a = [1, 2, 3, 4];
var length = a.length;
var boundary = 2;
var count = 0;
for (var i = length - 1; i >= 0; i--) {
if (a[i] > boundary) {
a[i] = null; // or delete a[i] ???
count++;
}
}
a.length -= count;
Or the same but using splice : 或者相同但使用拼接 :
for (var i = length - 1; i >= 0; i--) {
if (a[i] > boundary) {
a.splice(i, 1);
}
}
In modern engines, or with a shim, you can use Array.prototype.findIndex : 在现代引擎中,或使用垫片,您可以使用Array.prototype.findIndex :
var a = [1, 2, 3, 4]; var boundary = 2; a.length = a.findIndex( x => x > boundary ); console.log( a );
Since you're array is sorted you can also just use Array.prototype.filter : 由于您对数组进行了排序,因此您也可以使用Array.prototype.filter :
var a = [1, 2, 3, 4]; var boundary = 2; a = a.filter( x => x <= boundary ); console.log( a );
which may be slightly (negligibly) slower if the array is huge, but has more support in older engines. 如果阵列很大,可能会稍微(可忽略地)慢,但在旧引擎中有更多的支持。
Don't delete array entries at the end of an array to truncate it. 不要删除数组末尾的数组条目以截断它。 Deleting array entries removes them from the array by creating a sparse array: the length
property of the array remains unchanged: 删除数组条目通过创建稀疏数组将它们从数组中删除:数组的length
属性保持不变:
var a = [1,2,3,4]; console.log( "a.length %s", a.length); delete a[3]; console.log( "a[3] after deletion: %s", a[3]); console.log( "a.length after deletion %s", a.length); console.log( "a[3] still exists: %s", a.hasOwnProperty("3"));
Similar considerations apply to setting array entries to null
. 类似的注意事项适用于将数组条目设置为null
。 They will no longer test as truthy in a conditional expression, but have not been removed. 它们将不再在条件表达式中作为truthy测试,但尚未被删除。 As before the length of the array will remain unchanged. 和之前一样,数组的长度将保持不变。
Setting the length of the an array to something shorter than its existing value will truncate it in place. 将数组的长度设置为比其现有值更短的值将截断它。 There are many ways of finding the boundary position at which to truncate the array. 有许多方法可以找到截断数组的边界位置。 I suggest choosing one that appears natural to you and works in target browsers. 我建议选择一个对你来说很自然的,并在目标浏览器中工作。 EG using forSome
on a sorted array may not meet your requirements :) 在排序数组上使用forSome
EG可能不符合您的要求:)
var a = [1,2,3,4]; var boundary = 2; a.some( (c,i) => { if( c > boundary) { a.length = i; return true; } } ); console.log( a)
If using splice
, note that all entries from the boundary position to the end of array can be deleted in one call rather than deleting them one at a time: 如果使用splice
,请注意,可以在一次调用中删除从边界位置到数组末尾的所有条目,而不是一次删除一个:
var a = [1,2,3,4]; var boundary = 2; for( var i = a.length-1; a[i]>boundary; --i) {} // linear search a.splice(i+1); console.log(a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.