

JavaScript array value of the method
source link: https://www.codesd.com/item/javascript-array-value-of-the-method.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

JavaScript array value of the method
[].valueOf()
method retuns array itself.According to this
document.write([["a"]],["b"])
should return ['a']b isn't it?But this is not happening ,it just writes ab
.I just wanted to know the reason behind this.
For string elements .toString() method returns this,
["a","b"].toString()//a,b
But for elements with array it should return
[["a"],"b"].toString()//[a],b
When you pass an object to document.write, Javascript converts the object to a string with .toString(). In this case, Array.toString() will flatten and join the array with commas, and return it as a string.
["this", "is", "an", "array!"].toString(); // "this,is,an,array!"
[["a",["b"]], ["c"]].toString() // "a,b,c"
We can expand document.write([["a",["b"]], ["c"]])
into the following:
var input = [["a",["b"]], ["c"], "d"];
Array.prototype.verboseToString = function verboseToString() {
// Make a copy of the array, so we don't destroy the original
var copy = this.slice(), i;
for (i = 0; i < copy.length; i++) {
// If this is an Array, call verboseToString() on it, and go deeper
if (copy[i] instanceof Array === true) {
copy[i] = copy[i].verboseToString();
}
}
// copy contains non-arrays and we're ignoring other types' toString() output
return copy.join(',');
}
document.write(input.verboseToString()); // "a,b,c,d"
Recommend
-
11
Alongside the plain object, the array is a widely used data structure in JavaScript. And a widely used operation on arrays is accessing elements by index. In this post, I’m going to present the new array method array.at(index)
-
17
How to Use Array Reduce Method in JavaScriptLet’s say you have an array of numbers: const numbers = [2, 4, 6]; How can you sum these numbers? Thanks to the array.reduce() method...
-
7
Hi Everyone 👋 Today I wanted to share some of the most common use cases for Array sort() method in JavaScript. The...
-
6
Sort Array of Objects by String Property Value in Javascript While working with javascript objects and arrays, developers often need to sort the object array based on string property va...
-
6
Javascript: Sort an Array of Objects by Property Value A very common requirement developers encounter while working with javascript objects and arrays is to sort an array of objects. This article demonstrates easy ways...
-
6
Get a Random Value from Array in JavaScript Arrays are used to store data at the continuous memory location. Many people encounter a general requirement to get a random value from an array in javascript. This...
-
3
If you need to split up a string into an array of substrings, then you can use the JavaScript split() method. In this article, I will go over the JavaScript split() method and provide code examples.
-
12
How to Check If a Value Exists in an Array in JavaScript 1118 views 2 years ago Javascript Use the
-
6
-
4
Javascript Array Concat MethodNotificationsCheck what is trending in the tech world today! Tune in to TechBeat 🎵Yesterday at 4:56 PMRecently laid of...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK