xxxxxxxxxx
//create an array like so:
var colors = ["red","blue","green"];
//you can loop through an array like this:
for (var i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
xxxxxxxxxx
const language = ['Javascript', 'HTML,CSS', 'ReactJS']
// toString : Biến Array thàng 1 chuỗi
language.toString()
// join : Biến Array thành 1 chuỗi bằng cách đưa ra điểm chung
language.join()
// pop : Xoá element cuối array và trả về phần tử đã xóa
language.pop()
// push : Thêm 1 hoặc nhiều element cuối array và trả về độ dài mới của array
language.push('NodeJS')
// shift : Xóa đi phần tử ở đầu array
language.shift()
// Thêm 1 hoặc nhiều element đầu array và trả về độ dài mới của array
language.unshift('NodeJS')
// splice : Hàm dùng để xóa các phần tử trong array,
// Và thêm 1 hoặc nhiều element
// Tham số 1 vị trí bắt đầu đặt trỏ chuột xóa
// Tham số thứ 2 xóa bao nhiêu element (if 0 thì ko xóa)
// Tham số thứ 3
language.splice(1, 0, 'JAVA')
// concat : Dùng để nói 2 mảng với nhau
// language.concat(language2)
// slice : Dùng để cắt 1 hoặc nhiều element của array
// Dùng để copy array, có 2 tham số giống splice
language.slice(0, 2)
xxxxxxxxxx
//create an array:
let colors = ["red","blue","green"];
//you can loop through an array like this:
//For each color of the Array Colors
for (color of colors){
console.log(color);
}
//or you can loop through an array using the index:
for (var i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
xxxxxxxxxx
//create an array
let numbers = [ 11 , 13 , 15 , 17]
//you can use loop like this
for(let i = 0;i<numbers.length;i++) {
console.log(numbers[i])
}
xxxxxxxxxx
const array = [1, 2, 3, true, null, undefined];
console.log(array);
// expected output
// 1, 2, 3, true, null, undefined
Sostituire Items in posizioni specifiche
xxxxxxxxxx
let arr = ["Ciao", "da", "Francesco", "Sciuti"];
arr.splice(0, 2, "io", "sono");
console.log(arr) // output: ["io", "sono", "Francesco", "Sciuti"];
xxxxxxxxxx
// Match one d followed by one or more b's followed by one d
// Remember matched b's and the following d
// Ignore case
const myRe = /d(b+)(d)/i
const myArray = myRe.exec('cdbBdbsbz')
xxxxxxxxxx
//An array is a list of thing, you can add as many things as you want
var food = ["cake", "apple", "Ice-Cream"]
//You can use Math.Random to pick a random number
//from one to the length of the array
var Number = [Math.floor(Math.random() * food.length)]; // get a random number
//then we can console.log the random word from the array
console.log(logammounts[woodloot])
//You can also create an array, then provide the elements:
Example
const food = [];
food[0]= "cake";
food[1]= "apple";
food[2]= "Ice-Cream";