Search notes:

JavaScript: Array object

Array inherits from Function.

Methods and properties

get
prototype[@@unscopables]
prototype.length
prototype[@@iterator]()
prototype.at()
prototype.concat()
prototype.copyWithin()
prototype.entries()
prototype.every() Returns true if a function returns true for each element in the array. Compare with some()
prototype.fill()
prototype.filter()
prototype.find()
prototype.findIndex()
prototype.findLast()
prototype.findLastIndex()
prototype.flat()
prototype.flatMap()
prototype.forEach() Iterate over the elements of the array. Compare also with the forEach() method of NodeList and the for (e of a) and for (k in x) Javascript language construct.
from()
prototype.group() Experimental
prototype.groupToMap() Experimental
prototype.includes()
prototype.indexOf()
isArray()
prototype.join()
prototype.keys()
prototype.lastIndexOf()
prototype.map()
of()
prototype.pop()
prototype.push()
prototype.reduce()
prototype.reduceRight()
prototype.reverse()
prototype.shift()
prototype.slice() Compare with the slice() method of the String object.
prototype.some() Compare with every()
prototype.sort()
prototype.splice()
prototype.toLocaleString()
prototype.toString()
prototype.unshift()
prototype.values()

forEach

It's possible to modify the objects in the function that is called by forEach().
In the following example, the objects are added the new property txt:
let o1  = {num: 5};
let o2  = {num: 3};
let o3  = {num: 7};

[o1, o2, o3].forEach(o => o.txt = '*'.repeat(o.num));

print(o1.txt);
print(o2.txt);
print(o3.txt);

sort

js> let a = [
  { nam: 'abc', id: 5 },
  { nam: 'def', id: 2 },
  { nam: 'ghi', id: 9 },
  { nam: 'jkl', id: 3 },
  { nam: 'mno', id: 3 }
];

js> s = a.sort( (p, q) => p.id > q.id );

js> s.forEach( e => print(e.nam) );

reduce

js> let reduced = [4,2,1,9].reduce( (a, e) =>  { print(`a = ${a}, e = ${e}`); return a+e;} );
a = 4, e = 2
a = 6, e = 1
a = 7, e = 9

js> print(reduced);
16
Use an init value for accumulator a (here: 3):
js> let reduced = [4,2,1,9].reduce( (a, e) =>  { print(`a = ${a}, e = ${e}`); return a+e;}, 3 );

a = 3, e = 4
a = 7, e = 2
a = 9, e = 1
a = 10, e = 9

slice

js> let a = ['zero', 'one', 'two', 'three', 'four', 'five'];

js> a.slice()
["zero", "one", "two", "three", "four", "five"]

js> a.slice(1)
["one", "two", "three", "four", "five"]

js> a.slice(2)
["two", "three", "four", "five"]

js> a.slice(2,3)
["two"]

js> a.slice(2,4)
["two", "three"]

every

every(f) returns true if the function f returns true for all elements in the array:
js> a = [ 7, 5, 3, 9, 8 ];

js> a.every( e => e > 0 );
true

js> a.every( e => e > 5 );
false

concat

js> let a = ['zero', 'one', 'two', 'three', 'four', 'five'];

js> a.concat('six', 'seven')
["zero", "one", "two", "three", "four", "five", "six", "seven"]
js> [].concat(7, 3, 9);
[7, 3, 9]
js> [1,2].concat( [3, 4], [5, 6]);
[1, 2, 3, 4, 5, 6]

js> [1,2].concat( [[3, 4], [5, 6]], [7], [[8]]);
[1, 2, [3, 4], [5, 6], 7, [8]]

push

push adds an element to the end of an array and returns the number of elements in the array after adding the elements.
js> let a = [1, 2, 3];
js> a.push(4,5)
5
js> a
[1, 2, 3, 4, 5]

toString

<!DOCTYPE HTML>
<html>
<head>
  <title>Javasctipt Array.toString()</title>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">

  <script type="text/javascript">

    function init() {

      var out = document.getElementById('out');

      var arr = new Array(1, 42, 2014);

      out.innerHTML = arr.toString();  // 1,42,2014
    }

  </script>
</head>
<body onload="init();">

  <div id="out"></div>

</body>
</html>

flat

js> let a = 
[ 
  [1,2],
  [
    3,
    [4,5,
      [
       6,7
      ],
     8
    ]
  ]
];

js> a.flat()
[1, 2, 3, [4, 5, [6, 7], 8]]

js> a.flat().flat()
[1, 2, 3, 4, 5, [6, 7], 8]

js> a.flat().flat().flat()
[1, 2, 3, 4, 5, 6, 7, 8]
x.flat(n) is equivalent to applying flat() n times to x:
js> a.flat(2)
[1, 2, 3, 4, 5, [6, 7], 8]
It's possible to use Infinity for n:
js> a.flat(Infinity)
[1, 2, 3, 4, 5, 6, 7, 8]

Constructor

Note: new Array(42) creates an array with 42 elements while new Array(42, 99, 13) creates an array with 3 elements!
<!DOCTYPE html>
<html>
<head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <title>Javascript: Array constructor</title>

  <script type="text/javascript">
    
    function main() {

      var a1 = new Array(42);         // Create an array with 42 (undefined) elements
      var a2 = new Array(42, 99, 13); // create an array with 3 (defined) elements

      document.getElementById('out').innerHTML = 
          'a1.length = ' + a1.length + '<br>' +   // a1.length = 42
          'a2.length = ' + a2.length ;            // a2.length = 3

    }

  </script>

</head>
<body onload='main()';>

  <div id='out'>
  </div>
</body>
</html>
Github repository about-javascript, path: /objects/Array/constructor.html

See also

Create all element pairs of an array
objects

Index