Page 97 - JavaScript
P. 97
This returns:
[{
id: 1,
name: "John",
age: 28
},{
id: 2,
name: "Jane",
age: 31
}]
Joining array elements in a string
To join all of an array's elements into a string, you can use the join method:
console.log(["Hello", " ", "world"].join("")); // "Hello world"
console.log([1, 800, 555, 1234].join("-")); // "1-800-555-1234"
As you can see in the second line, items that are not strings will be converted first.
Converting Array-like Objects to Arrays
What are Array-like Objects?
JavaScript has "Array-like Objects", which are Object representations of Arrays with a length
property. For example:
var realArray = ['a', 'b', 'c'];
var arrayLike = {
0: 'a',
1: 'b',
2: 'c',
length: 3
};
Common examples of Array-like Objects are the arguments object in functions and HTMLCollection
or NodeList objects returned from methods like document.getElementsByTagName or
document.querySelectorAll.
However, one key difference between Arrays and Array-like Objects is that Array-like objects
inherit from Object.prototype instead of Array.prototype. This means that Array-like Objects can't
access common Array prototype methods like forEach(), push(), map(), filter(), and slice():
var parent = document.getElementById('myDropdown');
var desiredOption = parent.querySelector('option[value="desired"]');
var domList = parent.children;
domList.indexOf(desiredOption); // Error! indexOf is not defined.
domList.forEach(function() {
arguments.map(/* Stuff here */) // Error! map is not defined.
}); // Error! forEach is not defined.
https://riptutorial.com/ 54

