简体   繁体   English

使用javascript,更改锯齿状数组中元素长度的有效方法是什么?

[英]Using javascript, what is an efficient way of changing the length of elements in a jagged array?

I am trying to find an efficient way of changing the length of an array and the elements within the array.我试图找到一种有效的方法来改变数组的长度和数组中的元素。 For example, if I have例如,如果我有

var arr = [["Name", "Age", "Lightsaber Color"], ["Luke Skywalker", "22", "Green"], ["Yoda", "900", "Green"], ["Obi Wan Kenobi", "59", "Blue"]]

I want to end up with我想结束

var arr = [["Name", "Age"], ["Luke Skywalker", "22"], ["Yoda", "900"]]

I have written some code to do this, but I want to ensure that the way I am changing the length of the nested arrays (for lack of a better term) is as efficient as possible.我已经编写了一些代码来做到这一点,但我想确保我改变嵌套数组长度的方式(因为没有更好的术语)尽可能有效。

I have:我有:

var arr = [["Name", "Age", "Lightsaber Color"], ["Luke Skywalker", "22", "Green"], ["Yoda", "900", "Green"], ["Obi Wan Kenobi", "59", "Blue"]]
arr.length = 3; // Removes the Obi Wan entry
console.log(arr);
for(var i = 0; i < arr.length; i++){
        arr[i].length = 2 // Removes lightsaber color
};
console.log(arr)

The code I'm wanting to hopefully optimize is the for loop.我希望优化的代码是 for 循环。 I'll be working with much larger datasets.我将使用更大的数据集。 Thanks谢谢

The code I'm wanting to hopefully optimize is the for loop.我希望优化的代码是 for 循环。

What you're doing is already as efficient as it's going to get, if you mean runtime efficiency.如果您的意思是运行时效率,那么您正在做的事情已经和它将获得的一样高效。 Anything using other things (like slice or map ) is going to be slower.任何使用其他东西(如slicemap )的东西都会变慢。


Note that it's really unlikely to matter .请注意,这真的不太可能重要 Unless you've already identified that this loop is a performance blocker, don't worry about it.除非您已经确定此循环是性能障碍,否则不要担心。 If you have identified that this is a performance blocker, the array(s) must be really big .如果您已经确定,这是一个性能拦截器,阵列(或多个)必须是非常大的 :-) Even if you're dealing with 100,000 arrays, I wouldn't think this would take any significant time. :-) 即使您要处理 100,000 个数组,我也不认为这会花费大量时间。

Let's find out:让我们来了解一下:

 const now = performance.now ? performance.now.bind(performance) : Date.now.bind(Date); // Create an array with 110k entries, each being five entries long. const arr = Array.from({length:110000}, () => ["a", "b", "c", "d", "e"]); // Start timing const start = performance.now(); // Cut it down to 100k entries arr.length = 100000; // Trim them to two entries for (const entry of arr) { entry.length = 2; } // Done timing const elapsed = now() - start; console.log("Elapsed: " + elapsed + "ms");

I get about 20ms on my workstation using Brave (like Chrome, uses V8 as the JavaScript engine).我在我的工作站上使用 Brave(如 Chrome,使用 V8 作为 JavaScript 引擎)获得了大约 20 毫秒的时间。

This is not a rigorous benchmark , but it gives us an idea...不是一个严格的基准,但它给了我们一个想法......

Just a minor improvement, but sometimes Array.prototype.splice is faster than setting the array's length to truncate an array:只是一个小小的改进,但有时Array.prototype.splice比设置数组的长度来截断数组更快:

 var arr = [["Name", "Age", "Lightsaber Color"], ["Luke Skywalker", "22", "Green"], ["Yoda", "900", "Green"], ["Obi Wan Kenobi", "59", "Blue"]] arr.splice(3); // Removes the Obi Wan entry console.log(arr); for(var i = 0; i < arr.length; i++){ arr[i].splice(2) // Removes lightsaber color } console.log(arr);

For a benchmark comparing both versions, see http://jsben.ch/BmvNj .有关比较两个版本的基准,请参阅http://jsben.ch/BmvNj

You can use array.splice :您可以使用array.splice

arr[i] = arr[i].splice(0, 2);

Try this:尝试这个:

 var arr = [["Name", "Age", "Lightsaber Color"], ["Luke Skywalker", "22", "Green"], ["Yoda", "900", "Green"], ["Obi Wan Kenobi", "59", "Blue"]] arr.length = 3; // Removes the Obi Wan entry console.log(arr); for(var i = 0; i < arr.length; i++){ arr[i] = arr[i].splice(0, 2); }; console.log(arr)

Well, you can do this:好吧,你可以这样做:

arr.forEach(function(item) { item.splice(-1) });

splice(-1) modifies the original array, deleting the last item on it splice(-1)修改原始数组,删除其上的最后一项

You can combine Array.map with Array.slice :您可以将Array.mapArray.slice结合使用:

 let arr = [["Name", "Age", "Lightsaber Color"], ["Luke Skywalker", "22", "Green"], ["Yoda", "900", "Green"], ["Obi Wan Kenobi", "59", "Blue"]]; let result = arr.map(person => person.slice(0, 2)); console.log(result);

Here is a simple and readable way of your requirements.这是您的要求的一种简单易读的方式。

var arr = [["Name", "Age", "Lightsaber Color"], ["Luke Skywalker", "22", "Green"], ["Yoda", "900", "Green"], ["Obi Wan Kenobi", "59", "Blue"]]
    for(var i=0; i< arr.length;i++){
      arr[i].splice(-1, 1)
    }
    console.log(arr);

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

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