Dalam artikel sebelumnya "Artikel ini menerangkan beberapa kaedah pengendalian objek Objek dalam JS (kongsi) ", kami mengetahui tentang beberapa kaedah pengendalian objek Objek dalam JS. Artikel berikut akan membantu anda memahami beberapa kaedah pengendalian objek Array dalam JS Ia mempunyai nilai rujukan tertentu. Rakan yang memerlukan saya harap ia akan membantu anda.
javascript
中Array
Beberapa kaedah operasi yang cekap
kaedah daripada Cipta contoh tatasusunan baharu daripada tatasusunan atau objek boleh lelar.
console.log(Array.from("foo")); // expected output: Array ["f", "o", "o"] console.log(Array.from([1, 2, 3], (x) => x + x)); // expected output: Array [2, 4, 6]
digunakan untuk menentukan sama ada nilai yang dihantar ialah Array
.
Array.isArray([1, 2, 3]); // true Array.isArray({ foo: 123 }); // false Array.isArray("foobar"); // false Array.isArray(undefined); // false
Digunakan untuk memantau perubahan secara tak segerak dalam tatasusunan
Sintaks yang ditamatkan: Tatasusunan (arr, panggil balik)
mencipta tika tatasusunan baharu dengan bilangan argumen yang berubah-ubah, tanpa mengira bilangan atau jenis argumen. Kaedah
Array.of(7); // [7] Array.of(1, 2, 3); // [1, 2, 3] Array(7); // [ , , , , , , ] Array(1, 2, 3); // [1, 2, 3] //es5 if (!Array.of) { Array.of = function () { return Array.prototype.slice.call(arguments); }; }
digunakan untuk menggabungkan dua atau lebih tatasusunan. Kaedah ini tidak mengubah tatasusunan sedia ada, tetapi mengembalikan tatasusunan baharu. Kaedah
var array1 = ["a", "b", "c"]; var array2 = ["d", "e", "f"]; console.log(array1.concat(array2)); // expected output: Array ["a", "b", "c", "d", "e", "f"]
menyalin sebahagian tatasusunan ke lokasi lain dalam tatasusunan yang sama dan mengembalikannya tanpa mengubah saiznya. Kaedah
var array1 = [1, 2, 3, 4, 5]; // place at position 0 the element between position 3 and 4 console.log(array1.copyWithin(0, 3, 4)); // expected output: Array [4, 2, 3, 4, 5] // place at position 1 the elements after position 3 console.log(array1.copyWithin(1, 3)); // expected output: Array [4, 4, 5, 4, 5]
mengembalikan objek Array Iterator
baharu yang mengandungi pasangan kunci/nilai untuk setiap indeks dalam tatasusunan. Kaedah
var array1 = ["a", "b", "c"]; var iterator1 = array1.entries(); console.log(iterator1.next().value); // expected output: Array [0, "a"] console.log(iterator1.next().value); // expected output: Array [1, "b"]
menguji sama ada semua elemen tatasusunan melepasi ujian fungsi yang ditentukan. Kaedah
var array1 = [1, 30, 39, 29, 10, 13]; console.log(array1.every((x) => x < 40)); //out true
mengisi semua elemen dalam tatasusunan daripada indeks permulaan hingga indeks penamat dengan nilai tetap. Tidak termasuk penamatan
var array1 = [1, 2, 3, 4]; // fill with 0 from position 2 until position 4 console.log(array1.fill(0, 2, 4)); // expected output: [1, 2, 0, 0] // fill with 5 from position 1 console.log(array1.fill(5, 1)); // expected output: [1, 5, 5, 5] console.log(array1.fill(6)); // expected output: [6, 6, 6, 6]
mencipta tatasusunan baharu yang mengandungi semua elemen ujian yang dilaksanakan oleh fungsi yang disediakan. Kaedah
var words = ["spray", "limit", "elite", "exuberant", "destruction", "present"]; const result = words.filter((word) => word.length > 6); console.log(result); // expected output: Array ["exuberant", "destruction", "present"]
mengembalikan nilai elemen pertama dalam tatasusunan yang memenuhi fungsi ujian yang disediakan. Jika tidak, kembalikan undefined
. Kaedah
var array1 = [5, 12, 8, 130, 44]; var found = array1.find((x) => x > 10); console.log(found); // expected output: 12
mengembalikan indeks elemen pertama dalam tatasusunan yang memenuhi fungsi ujian yang disediakan. Jika tidak, kembalikan -1
. Kaedah
var array1 = [5, 12, 8, 130, 44]; var index = array1.findIndex((x) => x > 10); console.log(index); // expected output: 1
akan berulang ke kedalaman yang ditentukan untuk menyambung semua sub-tatasusunan dan mengembalikan tatasusunan baharu. Kaedah
var arr1 = [1, 2, [3, 4]]; arr1.flat(); // [1, 2, 3, 4] var arr2 = [1, 2, [3, 4, [5, 6]]]; arr2.flat(); // [1, 2, 3, 4, [5, 6]] var arr3 = [1, 2, [3, 4, [5, 6]]]; arr3.flat(2); // [1, 2, 3, 4, 5, 6] var arr4 = [1, 2, , 4, 5]; arr4.flat(); // [1, 2, 4, 5]
mula-mula memetakan setiap elemen menggunakan fungsi pemetaan dan kemudian memampatkan hasilnya ke dalam tatasusunan baharu. Ia hampir sama dengan map
dan nilai kedalaman 1
, tetapi fflat
biasanya lebih cekap sedikit apabila digabungkan menjadi satu kaedah. Kaedah latMap
var arr1 = [1, 2, 3, 4]; arr1.map((x) => [x * 2]); // [[2], [4], [6], [8]] arr1.flatMap((x) => [x * 2]); // [2, 4, 6, 8] // only one level is flattened arr1.flatMap((x) => [[x * 2]]); // [[2], [4], [6], [8]]
var array1 = ["a", "b", "c"]; array1.forEach((value, index, arr) => console.log(value)); // output 'a' // output 'b' // output 'c'
, jika tidak, ia akan kembali true
. Kaedah false
var array1 = [1, 2, 3]; console.log(array1.includes(2)); // expected output: true var pets = ["cat", "dog", "bat"]; console.log(pets.includes("cat")); // expected output: true console.log(pets.includes("at")); // expected output: false
jika ia tidak wujud. Kaedah -1
/var beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; console.log(beasts.indexOf('bison')); // expected output: 1 // start from index 2 console.log(beasts.indexOf('bison', 2)); // expected output: 4 console.log(beasts.indexOf('giraffe')); // expected output: -1
var elements = ["Fire", "Wind", "Rain"]; console.log(elements.join()); // expected output: Fire,Wind,Rain console.log(elements.join("")); // expected output: FireWindRain console.log(elements.join("-")); // expected output: Fire-Wind-Rain //数组[1,2,3,3,4,5]求和 eval([1, 2, 3, 3, 4, 5].join("+")) = 18;
Array
var array1 = ["a", "b", "c"]; var iterator = array1.keys(); for (let key of iterator) { console.log(key); // expected output: 0 1 2 }
dikembalikan. Cari ke hadapan dari belakang tatasusunan, bermula dari JavaScript
. Kaedah -1
fromIndex
var animals = ["Dodo", "Tiger", "Penguin", "Dodo"]; console.log(animals.lastIndexOf("Dodo")); // expected output: 3 console.log(animals.lastIndexOf("Tiger")); // expected output: 1
var array1 = [1, 4, 9, 16]; // pass a function to map const map1 = array1.map((x) => x * 2); console.log(map1); // expected output: Array [2, 8, 18, 32]
var plants = ["broccoli", "cauliflower", "cabbage", "kale", "tomato"]; console.log(plants.pop()); // expected output: "tomato" console.log(plants); // expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"] plants.pop(); console.log(plants); // expected output: Array ["broccoli", "cauliflower", "cabbage"]
var animals = ["pigs", "goats", "sheep"]; console.log(animals.push("cows")); // expected output: 4 console.log(animals); // expected output: Array ["pigs", "goats", "sheep", "cows"] animals.push("chickens"); console.log(animals); // expected output: Array ["pigs", "goats", "sheep", "cows", "chickens"]
const array1 = [1, 2, 3, 4]; const reducer = (accumulator, currentValue) => accumulator + currentValue; // 1 + 2 + 3 + 4 console.log(array1.reduce(reducer)); // expected output: 10 // 5 + 1 + 2 + 3 + 4 console.log(array1.reduce(reducer, 5)); // expected output: 15
accumulator
const array1 = [ [0, 1], [2, 3], [4, 5], ].reduceRight((accumulator, currentValue) => accumulator.concat(currentValue)); console.log(array1); // expected output: Array [4, 5, 2, 3, 0, 1]
var array1 = ["one", "two", "three"]; console.log("array1: ", array1); // expected output: Array ['one', 'two', 'three'] var reversed = array1.reverse(); console.log("reversed: ", reversed); // expected output: Array ['three', 'two', 'one'] /* Careful: reverse is destructive. It also changes the original array */ console.log("array1: ", array1); // expected output: Array ['three', 'two', 'one']
var array1 = [1, 2, 3]; var firstElement = array1.shift(); console.log(array1); // expected output: Array [2, 3] console.log(firstElement); // expected output: 1
var animals = ["ant", "bison", "camel", "duck", "elephant"]; console.log(animals.slice(2)); // expected output: Array ["camel", "duck", "elephant"] console.log(animals.slice(2, 4)); // expected output: Array ["camel", "duck"] console.log(animals.slice(1, 5)); // expected output: Array ["bison", "camel", "duck", "elephant"]
var array = [1, 2, 3, 4, 5]; var even = function (element) { // checks whether an element is even return element % 2 === 0; }; console.log(array.some(even)); // expected output: true
方法用原地算法对数组的元素进行排序,并返回数组。排序不一定是稳定的。默认排序顺序是根据字符串Unicode
码点。
var months = ["March", "Jan", "Feb", "Dec"]; months.sort(); console.log(months); // expected output: Array ["Dec", "Feb", "Jan", "March"] var array1 = [1, 30, 4, 21]; array1.sort(); console.log(array1); // expected output: Array [1, 21, 30, 4]
方法通过删除现有元素和/或添加新元素来更改一个数组的内容。
var months = ["Jan", "March", "April", "June"]; months.splice(1, 0, "Feb"); // 增 console.log(months); // expected output: Array ['Jan', 'Feb', 'March', 'April', 'June'] months.splice(4, 1, "May"); // 改 console.log(months); // expected output: Array ['Jan', 'Feb', 'March', 'April', 'May'] // 删 months.splice(4, 1); console.log(months); //output: ["Jan", "Feb", "March", "April"]
返回一个字符串表示数组中的元素。数组中的元素将使用各自的toLocaleString
方法转成字符串,这些字符串将使用一个特定语言环境的字符串(例如一个逗号 ",
")隔开。
var array1 = [1, "a", new Date("21 Dec 1997 14:12:00 UTC")]; var localeString = array1.toLocaleString("en", { timeZone: "UTC" }); console.log(localeString); // expected output: "1,a,12/21/1997, 2:12:00 PM", // This assumes "en" locale and UTC timezone - your results may vary var prices = ["¥7", 500, 8123, 12]; prices.toLocaleString("ja-JP", { style: "currency", currency: "JPY" }); // "¥7,¥500,¥8,123,¥12"
返回一个字符串,代表该数组的源代码。
该特性是非标准的,请尽量不要在生产环境中使用它!
var alpha = new Array("a", "b", "c"); alpha.toSource(); //返回["a", "b", "c"]
返回一个字符串,表示指定的数组及其元素。
var array1 = [1, 2, "a", "1a"]; console.log(array1.toString()); // expected output: "1,2,a,1a"
方法将一个或多个元素添加到数组的开头,并返回新数组的长度。
var array1 = [1, 2, 3]; console.log(array1.unshift(4, 5)); // expected output: 5 console.log(array1); // expected output: Array [4, 5, 1, 2, 3]
方法返回一个新的Array Iterator
对象,该对象包含数组每个索引的值。
const array1 = ["a", "b", "c"]; const iterator = array1.values(); for (const value of iterator) { console.log(value); // expected output: "a" "b" "c" }
推荐学习:JavaScript视频教程
Atas ialah kandungan terperinci Analisis ringkas beberapa kaedah pengendalian objek Array dalam JS (dengan kod). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!