| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Indexed_collections | [Back] [Original] |
Get to know MDN better
emp emp[0] 0 emp[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
0
// ...
const arr1 = new Array(arrayLength);
// ...
const arr2 = Array(arrayLength);
//
const arr3 = [];
arr3.length = arrayLength;
arrayLength Number arr.length arrayLengthfor...in
const obj = {};
//
obj.prop = [element0, element1, /* ,*/ elementN];
//
const obj = { prop: [element0, element1, /* ,*/ elementN] };
Number Number Array() arrayLength
// 42
const arr = [42];
// 42
const arr = Array(42);
//
const arr = [];
arr.length = 42;
N Array(N) RangeError
const arr = Array(9.3); // RangeError: Invalid array length
const wisenArray = Array.of(9.3); // wisenArray 9.3
const myArray = ["Wind", "Rain", "Fire"];
myArray[0] 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 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.length = 3;
console.log(cats); // [ <3 empty items> ]
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 forEach
forEach undefined
const sparseArray = ["first", "second", , "fourth"];
sparseArray.forEach((element) => {
console.log(element);
});
// first
// second
// fourth
if (sparseArray[2] === undefined) {
console.log("sparseArray[2] undefined"); // true
}
const nonsparseArray = ["first", "second", undefined, "fourth"];
nonsparseArray.forEach((element) => {
console.log(element);
});
// first
// second
// undefined
// fourth
JavaScript for...in JavaScript
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() length
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"myArray
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()
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; // 0 => a = ba b
};
myArray.sort(sortFn);
// myArray = ["Wind","Fire","Rain"]
a b -1a b 1a b 0indexOf() 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 thisArgthisArg this this undefinedthis windowglobalThis
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
callback true every() 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
callback true some() 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)reduce callback
initialValue callback initialValue
initialValue callback callback
callback
const a = [10, 20, 30];
const total = a.reduce(
(accumulator, currentValue) => accumulator + currentValue,
0,
);
console.log(total); // 60
reduceRight() reduce()
reduce reduceRight
undefined
// Array
const a = Array(5); // [ <5 empty items> ]
//
const b = [1, 2, , , 5]; // [ 1, 2, <2 empty items>, 5 ]
// array.length
const c = [1, 2];
c[4] = 5; // [ 1, 2, <2 empty items>, 5 ]
// .length
const d = [1, 2];
d.length = 5; // [ 1, 2, <3 empty items> ]
//
const e = [1, 2, 3, 4, 5];
delete e[2]; // [ 1, 2, <1 empty item>, 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 empty items>, 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
//
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
const a = new Array(4);
for (i = 0; i < 4; i++) {
a[i] = new Array(4);
for (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()
JavaScript 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 |