[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Indexed_collections#sparse_arrays [Back]  [Original]

- JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Array TypedArray

emp emp[0] 0emp[1] 1

JavaScript Array Array

js
const arr1 = new Array(element0, element1, /* , */ elementN);
const arr2 = Array(element0, element1, /* , */ elementN);
const arr3 = [element0, element1, /* , */ elementN];

element0, element1, , elementN length

js
// ...
const arr1 = new Array(arrayLength);

// ...
const arr2 = Array(arrayLength);

// 
const arr3 = [];
arr3.length = arrayLength;

: arrayLength Numberarr.length arrayLength for...in

js
const obj = {};
// 
obj.prop = [element0, element1, /* , */ elementN];

// 
const obj = { prop: [element0, element1, /* , */ elementN] };

Number Number Array() arrayLength

42

js
const arr = [42];

arr.length 42

js
const arr = Array(42);

js
const arr = [];
arr.length = 42;

N Array(N) RangeError

js
const arr = Array(9.3); // RangeError: Invalid array length

ES2015 Array.of

js
const arr = Array.of(9.3); // arr  1  9.3 

js
const myArray = ["Wind", "Rain", "Fire"];

0 1 myArray[0]2 myArray[1]

:

js
const arr = ["one", "two", "three"];
arr[2]; // three
arr["length"]; // 3

js
const emp = [];
emp[0] = "Casey Jones";
emp[1] = "Phil Lesh";
emp[2] = "August West";

:

js
const arr = [];
arr[3.4] = "Oranges";
console.log(arr.length); // 0
console.log(Object.hasOwn(arr, 3.4)); // true

js
const myArray = new Array("Hello", myVar, 3.14159);
// 
const myArray = ["Mango", "Apple", "Orange"];

JavaScript

length 'Dusty' 30 cats.length 30 + 1

JavaScript 0 1 length 1

js
const cats = [];
cats[30] = ["Dusty"];
console.log(cats.length); // 31

length

0

js
const cats = ["Dusty", "Misty", "Twiggy"];
console.log(cats.length); // 3

cats.length = 2;
console.log(cats); // [ 'Dusty', 'Misty' ] - Twiggy 

cats.length = 0;
console.log(cats); // [];  cats 

cats.length = 3;
console.log(cats); // [ <3 > ]

js
const colors = ["red", "green", "blue"];
for (let i = 0; i < colors.length; i++) {
  console.log(colors[i]);
}

false DOM

js
const divs = document.getElementsByTagName("div");
for (let i = 0, div; (div = divs[i]); i++) {
  /* div  */
}

div

forEach()

js
const colors = ["red", "green", "blue"];
colors.forEach((color) => console.log(color));
// red
// green
// blue

forEach 1 forEach

forEach undefined

js
const sparseArray = ["first", "second", , "fourth"];

sparseArray.forEach((element) => {
  console.log(element);
});
// Logs:
// first
// second
// fourth

if (sparseArray[2] === undefined) {
  console.log("sparseArray[2] is undefined"); // true
}

const nonsparseArray = ["first", "second", undefined, "fourth"];

nonsparseArray.forEach((element) => {
  console.log(element);
});
// Logs:
// first
// second
// undefined
// fourth

JavaScript for...in JavaScript

Array

concat() 2

js
let myArray = ["1", "2", "3"];
myArray = myArray.concat("a", "b", "c");
// myArray  ["1", "2", "3", "a", "b", "c"] 

join()

js
const myArray = ["Wind", "Rain", "Fire"];
const list = myArray.join(" - "); // list  "Wind - Rain - Fire" 

push() 1

js
const myArray = ["1", "2"];
myArray.push("3"); // myArray  ["1", "2", "3"] 

pop()

js
const myArray = ["1", "2", "3"];
const last = myArray.pop();
// myArray  ["1", "2"] last  "3" 

shift()

js
const myArray = ["1", "2", "3"];
const first = myArray.shift();
// myArray  ["2", "3"]first  "1" 

unshift() 1

js
const myArray = ["1", "2", "3"];
myArray.unshift("4", "5");
// myArray  ["4", "5", "1", "2", "3"] 

slice()

js
let myArray = ["a", "b", "c", "d", "e"];
myArray = myArray.slice(1, 4); // [ "b", "c", "d"]
//  1  3 
// 

at() undefined

js
const myArray = ["a", "b", "c", "d", "e"];
myArray.at(-2); // "d", the second-last element of myArray

splice() ()

js
const myArray = ["1", "2", "3", "4", "5"];
myArray.splice(1, 3, "a", "b", "c", "d");
// myArray  ["1", "a", "b", "c", "d", "5"] 
//  1 "2" 
// 3 

reverse()

js
const myArray = ["1", "2", "3"];
myArray.reverse();
// myArray = ["3", "2", "1"] 

flat()

js
let myArray = [1, 2, [3, 4]];
myArray = myArray.flat();
// myArray  [1, 2, 3, 4]  [3, 4] 

sort()

js
const myArray = ["Wind", "Rain", "Fire"];
myArray.sort();
// myArray = ["Fire", "Rain", "Wind"] 

sort() 2 2 2 0

js
const sortFn = (a, b) => {
  if (a[a.length - 1] < b[b.length - 1]) {
    return -1; //  => a < ba  b 
  } else if (a[a.length - 1] > b[b.length - 1]) {
    return 1; //  => a > ba  b 
  }
  return 0; //  => a = ba  b 
};
myArray.sort(sortFn);
//  myArray = ["Wind","Fire","Rain"] 
  • a b -1
  • a b 1
  • a b 0

indexOf() searchElement

js
const a = ["a", "b", "a", "b", "a"];
console.log(a.indexOf("b")); // 1 

// 
console.log(a.indexOf("b", 2)); // 3
console.log(a.indexOf("z")); // -1'z' 

lastIndexOf() indexOf

js
const a = ["a", "b", "c", "d", "a", "b"];
console.log(a.lastIndexOf("b")); // 5

// 
console.log(a.lastIndexOf("b", 4)); // 1
console.log(a.lastIndexOf("z")); // -1

forEach() callback undefined

js
const a = ["a", "b", "c"];
a.forEach((element) => {
  console.log(element);
});
// :
// a
// b
// c

forEach thisArg 2 thisArg this this windowglobalThis undefined

: sort() sort() thisArg

map() callback

js
const a1 = ["a", "b", "c"];
const a2 = a1.map((item) => item.toUpperCase());
console.log(a2); // ['A', 'B', 'C']

flatMap() map() 1 flat()

js
const a1 = ["a", "b", "c"];
const a2 = a1.flatMap((item) => [item.toUpperCase(), item.toLowerCase()]);
console.log(a2); // ['A', 'a', 'B', 'b', 'C', 'c']

filter() callback true

js
const a1 = ["a", 10, "b", 20, "c", 30];
const a2 = a1.filter((item) => typeof item === "number");
console.log(a2); // [10, 20, 30]

find() callback true

js
const a1 = ["a", 10, "b", 20, "c", 30];
const i = a1.find((item) => typeof item === "number");
console.log(i); // 10

findLast() callback true

js
const a1 = ["a", 10, "b", 20, "c", 30];
const i = a1.findLast((item) => typeof item === "number");
console.log(i); // 30

findIndex() callback true

js
const a1 = ["a", 10, "b", 20, "c", 30];
const i = a1.findIndex((item) => typeof item === "number");
console.log(i); // 1

findLastIndex() callback true

js
const a1 = ["a", 10, "b", 20, "c", 30];
const i = a1.findLastIndex((item) => typeof item === "number");
console.log(i); // 5

every() callback true true

js
function isNumber(value) {
  return typeof value === "number";
}
const a1 = [1, 2, 3];
console.log(a1.every(isNumber)); // true
const a2 = [1, "2", 3];
console.log(a2.every(isNumber)); // false

some() callback true true

js
function isNumber(value) {
  return typeof value === "number";
}
const a1 = [1, 2, 3];
console.log(a1.some(isNumber)); // true
const a2 = [1, "2", 3];
console.log(a2.some(isNumber)); // true
const a3 = ["1", "2", "3"];
console.log(a3.some(isNumber)); // false

reduce() callback(accumulator, currentValue, currentIndex, array) 1 reduce callback

initialValue initialValue 1 2 callback

initialValue callback 2 2 callback 2

callback

js
const a = [10, 20, 30];
const total = a.reduce(
  (accumulator, currentValue) => accumulator + currentValue,
  0,
);
console.log(total); // 60

reduceRight() reduce()

reduce reduceRight 2

Object.groupBy()

name type "food"

js
const inventory = [
  { name: "asparagus", type: "vegetables" },
  { name: "bananas", type: "fruit" },
  { name: "goat", type: "meat" },
  { name: "cherries", type: "fruit" },
  { name: "fish", type: "meat" },
];

Object.groupBy()

js
const result = Object.groupBy(inventory, ({ type }) => type);
console.log(result);
// Logs
// {
//   vegetables: [{ name: 'asparagus', type: 'vegetables' }],
//   fruit: [
//     { name: 'bananas', type: 'fruit' },
//     { name: 'cherries', type: 'fruit' }
//   ],
//   meat: [
//     { name: 'goat', type: 'meat' },
//     { name: 'fish', type: 'meat' }
//   ]
// }

Map.groupBy() Map Object.groupBy()

undefined

js
// Array :
const a = Array(5); // [ <5 > ]

// :
const b = [1, 2, , , 5]; // [ 1, 2, <2 >, 5 ]

// array.length :
const c = [1, 2];
c[4] = 5; // [ 1, 2, <2 >, 5 ]

// .length :
const d = [1, 2];
d.length = 5; // [ 1, 2, <3 > ]

// :
const e = [1, 2, 3, 4, 5];
delete e[2]; // [ 1, 2, <1 >, 4, 5 ]

undefined

js
const arr = [1, 2, , , 5]; // 

// 
console.log(arr[2]); // undefined

// For...of
for (const i of arr) {
  console.log(i);
}
// : 1 2 undefined undefined 5

// 
const another = [...arr]; // "another"  [ 1, 2, undefined, undefined, 5 ]

js
const mapped = arr.map((i) => i + 1); // [ 2, 3, <2 >, 6 ]
arr.forEach((i) => console.log(i)); // 1 2 5
const filtered = arr.filter(() => true); // [ 1, 2, 5 ]
const hasFalsy = arr.some((k) => !k); // false

// Property enumeration
const keys = Object.keys(arr); // [ '0', '1', '4' ]
for (const key in arr) {
  console.log(key);
}
// : '0' '1' '4'
// 
const objectSpread = { ...arr }; // { '0': 1, '1': 2, '4': 5 }

Array

JavaScript

2

js
const a = new Array(4);
for (let i = 0; i < 4; i++) {
  a[i] = new Array(4);
  for (let j = 0; j < 4; j++) {
    a[i][j] = `[${i}, ${j}]`;
  }
}

Row 0: [0, 0] [0, 1] [0, 2] [0, 3]
Row 1: [1, 0] [1, 1] [1, 2] [1, 3]
Row 2: [2, 0] [2, 1] [2, 2] [2, 3]
Row 3: [3, 0] [3, 1] [3, 2] [3, 3]

js
const arr = [1, 2, 3];
arr.property = "value";
console.log(arr.property); // "value"

RegExp.prototype.exec(), String.prototype. match(), String.prototype.split()

document.getElementsByTagName() NodeList arguments JavaScript arguments length forEach()

js
function printArguments() {
  arguments.forEach((item) => {
    console.log(item);
  }); // TypeError: arguments.forEach is not a function
}

Function.prototype.call()

js
function printArguments() {
  Array.prototype.forEach.call(arguments, (item) => {
    console.log(item);
  });
}

js
Array.prototype.forEach.call("a string", (chr) => {
  console.log(chr);
});

Web Proxy Viewer  |  New URL  |  Original Page