Arrays

  • 4.1 Use the literal syntax for array creation. eslint: no-array-constructor

    1. // bad
    2. const items = new Array();
    3. // good
    4. const items = [];

  • 4.2 Use Array#push instead of direct assignment to add items to an array.

    1. const someStack = [];
    2. // bad
    3. someStack[someStack.length] = 'abracadabra';
    4. // good
    5. someStack.push('abracadabra');

  • 4.3 Use array spreads ... to copy arrays.

    1. // bad
    2. const len = items.length;
    3. const itemsCopy = [];
    4. let i;
    5. for (i = 0; i < len; i += 1) {
    6. itemsCopy[i] = items[i];
    7. }
    8. // good
    9. const itemsCopy = [...items];


  • 4.4 To convert an iterable object to an array, use spreads ... instead of Array.from.

    1. const foo = document.querySelectorAll('.foo');
    2. // good
    3. const nodes = Array.from(foo);
    4. // best
    5. const nodes = [...foo];

  • 4.5 Use Array.from for converting an array-like object to an array.

    1. const arrLike = { 0: 'foo', 1: 'bar', 2: 'baz', length: 3 };
    2. // bad
    3. const arr = Array.prototype.slice.call(arrLike);
    4. // good
    5. const arr = Array.from(arrLike);

  • 4.6 Use Array.from instead of spread ... for mapping over iterables, because it avoids creating an intermediate array.

    1. // bad
    2. const baz = [...foo].map(bar);
    3. // good
    4. const baz = Array.from(foo, bar);

  • 4.7 Use return statements in array method callbacks. It’s ok to omit the return if the function body consists of a single statement returning an expression without side effects, following 8.2. eslint: array-callback-return

    1. // good
    2. [1, 2, 3].map((x) => {
    3. const y = x + 1;
    4. return x * y;
    5. });
    6. // good
    7. [1, 2, 3].map(x => x + 1);
    8. // bad - no returned value means `acc` becomes undefined after the first iteration
    9. [[0, 1], [2, 3], [4, 5]].reduce((acc, item, index) => {
    10. const flatten = acc.concat(item);
    11. acc[index] = flatten;
    12. });
    13. // good
    14. [[0, 1], [2, 3], [4, 5]].reduce((acc, item, index) => {
    15. const flatten = acc.concat(item);
    16. acc[index] = flatten;
    17. return flatten;
    18. });
    19. // bad
    20. inbox.filter((msg) => {
    21. const { subject, author } = msg;
    22. if (subject === 'Mockingbird') {
    23. return author === 'Harper Lee';
    24. } else {
    25. return false;
    26. }
    27. });
    28. // good
    29. inbox.filter((msg) => {
    30. const { subject, author } = msg;
    31. if (subject === 'Mockingbird') {
    32. return author === 'Harper Lee';
    33. }
    34. return false;
    35. });

  • 4.8 Use line breaks after open and before close array brackets if an array has multiple lines

    1. // bad
    2. const arr = [
    3. [0, 1], [2, 3], [4, 5],
    4. ];
    5. const objectInArray = [{
    6. id: 1,
    7. }, {
    8. id: 2,
    9. }];
    10. const numberInArray = [
    11. 1, 2,
    12. ];
    13. // good
    14. const arr = [[0, 1], [2, 3], [4, 5]];
    15. const objectInArray = [
    16. {
    17. id: 1,
    18. },
    19. {
    20. id: 2,
    21. },
    22. ];
    23. const numberInArray = [
    24. 1,
    25. 2,
    26. ];