简体   繁体   English

JavaScript - 截断数组的更有效方法

[英]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);
    }
}
 a.length -= count;

Thats sufficient ;) (as length is not a simple property, but rather a getter/setter which truncates/adds elements (ref) ) 这足够了;)(因为长度不是一个简单的属性,而是一个截断/添加元素的getter / setter (ref)

Alternatively use splice: 或者使用拼接:

 a.splice(-count, count);

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.

相关问题 更有效的搜索javascript对象数组的方法? - More efficient way to search an array of javascript objects? 有没有更高效的方法来迭代这个数组? (JavaScript) - Is there a more run-time efficient way to iterate through this array? (JavaScript) Javascript-有没有更有效的方法来创建数组数组? -提供的例子 - Javascript - Is there a more efficient way to create an array of arrays? - Examples provided 在 javascript 中编码的更有效方法 - more efficient way to code this in javascript 更有效地编写这个javascript的方法 - More efficient way of writing this javascript 有没有更有效的方法来排序这个数组? - Is there a more efficient way to sort this array? 有没有一种更有效的方法可以对对象中的数组中的数组进行排序? - Is there a more efficient way to sort an Array in an Array in an Object? 在Javascript中更新对象数组中的某些元素时,考虑到数组大小,哪一个更有效? - When updating some of the elements in an object array in Javascript, which one is a more efficient way considering the array size? 使用Javascript更有效地改变样式的方法? - More efficient way to change styles using Javascript? 有没有更有效的方法来改变 HTML 文本与 JavaScript - Is There a More Efficient Way to Alter HTML Text with JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM