| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Indexed_collections | [Back] [Original] |
Get to know MDN better
emp emp[0] 0emp[1] 1
JavaScript Array Array
const arr1 = new Array(element0, element1, /* , */ elementN);
const arr2 = Array(element0, element1, /* , */ elementN);
const arr3 = [element0, element1, /* , */ elementN];
element0, element1, , elementN length
// ...
const arr1 = new Array(arrayLength);
// ...
const arr2 = Array(arrayLength);
//
const arr3 = [];
arr3.length = arrayLength;
:
arrayLength Numberarr.length arrayLength for...in
const obj = {};
//
obj.prop = [element0, element1, /* , */ elementN];
//
const obj = { prop: [element0, element1, /* , */ elementN] };
Number Number Array() arrayLength
42
const arr = [42];
arr.length 42
const arr = Array(42);
const arr = [];
arr.length = 42;
N Array(N) RangeError
const arr = Array(9.3); // RangeError: Invalid array length
ES2015 Array.of
const arr = Array.of(9.3); // arr 1 9.3
const myArray = ["Wind", "Rain", "Fire"];
0 1 myArray[0]2 myArray[1]
const emp = [];
emp[0] = "Casey Jones";
emp[1] = "Phil Lesh";
emp[2] = "August West";
:
const arr = [];
arr[3.4] = "Oranges";
console.log(arr.length); // 0
console.log(Object.hasOwn(arr, 3.4)); // true
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
const cats = [];
cats[30] = ["Dusty"];
console.log(cats.length); // 31
length
0
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 > ]
const colors = ["red", "green", "blue"];
for (let i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
false DOM
const divs = document.getElementsByTagName("div");
for (let i = 0, div; (div = divs[i]); i++) {
/* div */
}
div
const colors = ["red", "green", "blue"];
colors.forEach((color) => console.log(color));
// red
// green
// blue
forEach 1 forEach
forEach undefined
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
concat() 2
let myArray = ["1", "2", "3"];
myArray = myArray.concat("a", "b", "c");
// myArray ["1", "2", "3", "a", "b", "c"]
const myArray = ["Wind", "Rain", "Fire"];
const list = myArray.join(" - "); // list "Wind - Rain - Fire"
push() 1
const myArray = ["1", "2"];
myArray.push("3"); // myArray ["1", "2", "3"]
const myArray = ["1", "2", "3"];
const last = myArray.pop();
// myArray ["1", "2"] last "3"
const myArray = ["1", "2", "3"];
const first = myArray.shift();
// myArray ["2", "3"]first "1"
const myArray = ["1", "2", "3"];
myArray.unshift("4", "5");
// myArray ["4", "5", "1", "2", "3"]
let myArray = ["a", "b", "c", "d", "e"];
myArray = myArray.slice(1, 4); // [ "b", "c", "d"]
// 1 3
//
at() undefined
const myArray = ["a", "b", "c", "d", "e"];
myArray.at(-2); // "d", the second-last element of myArray
splice() ()
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
const myArray = ["1", "2", "3"];
myArray.reverse();
// myArray = ["3", "2", "1"]
let myArray = [1, 2, [3, 4]];
myArray = myArray.flat();
// myArray [1, 2, 3, 4] [3, 4]
const myArray = ["Wind", "Rain", "Fire"];
myArray.sort();
// myArray = ["Fire", "Rain", "Wind"]
sort() 2 2 2 0
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 -1a b 1a b 0 indexOf() searchElement
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
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
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
const a1 = ["a", "b", "c"];
const a2 = a1.map((item) => item.toUpperCase());
console.log(a2); // ['A', 'B', 'C']
flatMap() map() 1 flat()
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
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
const a1 = ["a", 10, "b", 20, "c", 30];
const i = a1.find((item) => typeof item === "number");
console.log(i); // 10
findLast() callback true
const a1 = ["a", 10, "b", 20, "c", 30];
const i = a1.findLast((item) => typeof item === "number");
console.log(i); // 30
findIndex() callback true
const a1 = ["a", 10, "b", 20, "c", 30];
const i = a1.findIndex((item) => typeof item === "number");
console.log(i); // 1
findLastIndex() callback true
const a1 = ["a", 10, "b", 20, "c", 30];
const i = a1.findLastIndex((item) => typeof item === "number");
console.log(i); // 5
every() callback true true
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
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
const a = [10, 20, 30];
const total = a.reduce(
(accumulator, currentValue) => accumulator + currentValue,
0,
);
console.log(total); // 60
reduceRight() reduce()
reduce reduceRight 2
name type "food"
const inventory = [
{ name: "asparagus", type: "vegetables" },
{ name: "bananas", type: "fruit" },
{ name: "goat", type: "meat" },
{ name: "cherries", type: "fruit" },
{ name: "fish", type: "meat" },
];
Object.groupBy()
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
// 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
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 ]
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 }
JavaScript
2
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]
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()
function printArguments() {
arguments.forEach((item) => {
console.log(item);
}); // TypeError: arguments.forEach is not a function
}
function printArguments() {
Array.prototype.forEach.call(arguments, (item) => {
console.log(item);
});
}
Array.prototype.forEach.call("a string", (chr) => {
console.log(chr);
});
| Web Proxy Viewer | New URL | Original Page |