简体   繁体   English

我怎样才能真正理解'Array.prototype.join'的方法

[英]How can I really understand the method of 'Array.prototype.join'

I use the method of Array.prototype.join for multidimensional array, but I get curious result. 我对多维数组使用Array.prototype.join方法,但得到的结果很奇怪。 I try to see the bottom implementation of Array.prototype.join , it show me the native code 我尝试查看Array.prototype.join的底部实现,它向我展示了本机代码

[1,2,3,4,5,6,7,8].join('')
[1, [2], [3, [4, [5]]], [6, [7, [8]]]].join('');

I expect the output of 我希望输出

[1,[2],[3,[4,[5]]],[6,[7,[8]]]].join('') 

to be 成为

12345678 12345678

but the actual output is 但实际输出是

123,4,56,7,8 123,4,56,7,8

.join在每个元素上调用.toString()以将其转换为字符串,并且Array.prototype.toString()默认情况下将其所有元素与,

 console.log([1, 2, 3].toString()); 

Like others already said, toString is called upon the array. 就像其他人已经说过的, toString在数组上被调用。 However, by providing a separator it will only join the array on which you call the method with that separator. 但是,通过提供分隔符,它将仅连接使用该分隔符在其上调用方法的数组。

[
  1, 
  [2], 
  [3, [4, [5]]], 
  [6, [7, [8]]]
].join("")

Will simply join the first level elements together, calling toString on elements that aren't a string. 将简单地将第一级元素连接在一起,对不是字符串的元素调用toString

1.toString() + "" +
 [2].toString() + "" +
 [3, [4, [5]]].toString() + "" +
 [6, [7, [8]]].toString()

Where the "" is the separator. 其中""是分隔符。 Resulting in 123,4,56,7,8 where 3,4,5 is the first nested array and 6,7,8 the second (with more than 1 element). 结果为123,4,56,7,8 ,其中3,4,5是第一个嵌套数组,而6,7,8是第二个嵌套数组(具有多个元素)。 If you'd like to join the elements recursive you could create your own method. 如果您想加入递归元素,则可以创建自己的方法。

 Array.prototype.deepJoin = function (separator) { return this .map(e => Array.isArray(e) ? e.deepJoin(separator) : e) .join(separator); }; var array = [1, [2], [3, [4, [5]]], [6, [7, [8]]]]; console.log(array.join()); console.log(array.deepJoin()); console.log(array.join("")); console.log(array.deepJoin("")); console.log(array.join(" - ")); console.log(array.deepJoin(" - ")); 

The join method also invoke toString() on each element. join方法还会在每个元素上调用toString()

You have 你有

[2].toString() -> 2
[3, [4, 5]].toString() -> 3,4,5

And so on. 等等。 The toString method on an array never preserves square parenthesis 数组上的toString方法从不保留方括号

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

相关问题 在Javascript中,Array.join真的是Array.prototype.join吗? - In Javascript, is Array.join really Array.prototype.join? 如何在reactjs中使用array.prototype.join - How to using array.prototype.join in reactjs Array.prototype.join在IE 8中不起作用 - Array.prototype.join not working in IE 8 为什么 Array.prototype.join() 会展平任意深度的数组? - Why does Array.prototype.join() flatten an array of any depth? 类似于Java中的Array.prototype.join的地图或平面图 - Map or flatmap that works like Array.prototype.join in Javascript Array.prototype.join() 在反应 useState 中不起作用 - Array.prototype.join() doesn't work in react useState 除了Array.prototype.join()以外,还有没有更短的方法将数组连接到一个字符串? - Is there a shorter way to join an array to one string besides Array.prototype.join()? 我尝试编写自己的Array.prototype.join()有什么问题? - What's wrong with my attempt to write my own Array.prototype.join()? 为什么 Array.prototype.map 会忽略稀疏数组中的空槽,而 Array.prototype.join 不会? - Why does Array.prototype.map ignore empty slots in a sparse array whereas Array.prototype.join does not? 我可以将 forEach 方法从 Array 原型添加到 String 原型吗? - Can I add forEach method to String prototype from Array prototype?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM