Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
听说map函数的计算量是O(1)。但我无法理解原因。
如果我正确理解您的问题,O(1) 是访问一项的复杂性。JS 中的 Array.map() 将当前值传递给函数并遍历它们,并获取函数的返回值并将其插入新数组中。
因此,该函数循环遍历数组中的每个对象,复杂度为 O(n)。
例如:
[1, 2, 3].map(function (item) { return item + 1; });
所述函数一次获取一项,访问数组 n 次 (3)。
编辑:看起来我误解了你的问题,我的错。