Javascript Array
myArray.length; myArray[0]; // the first item in the array myArray[1]; // the second item in the array myArray[myArray.length-1]; // the last item in the array
let myNewString = myArray.join(',');
myNewString;let dogNames = ['Rocket','Flash','Bella','Slugger']; dogNames.toString(); // Rocket,Flash,Bella,Slugger
Add remove array items
push()
pop()
let myArray = ['Manchester', 'London', 'Liverpool', 'Birmingham', 'Leeds', 'Carlisle'];
myArray.push('Cardiff');
myArray;
myArray.push('Bradford', 'Brighton');
myArray;
从头开始加入和删除
unshift() shift()
const list = document.querySelector('.output ul');
const searchInput = document.querySelector('.output input');
const searchBtn = document.querySelector('.output button');
list.innerHTML = '';
let myHistory = [];
searchBtn.onclick = function() {
// we will only allow a term to be entered if the search input isn't empty
if (searchInput.value !== '') {
// number 1
myHistory.unshift(searchInput.value);
// empty the list so that we don't display duplicate entries
// the display is regenerated every time a search term is entered.
list.innerHTML = '';
// loop through the array, and display all the search terms in the list
for (let i = 0; i < myHistory.length; i++) {
itemText = myHistory[i];
const listItem = document.createElement('li');
listItem.textContent = itemText;
list.appendChild(listItem);
}
// If the array length is 5 or more, remove the oldest search term
if (myHistory.length >= 5) {
// number 2
myHistory.pop();
}
// empty the search input and focus it, ready for the next term to be entered
searchInput.value = '';
searchInput.focus();
}
}splice 剪接
reverse 颠倒
sort 排序
let sortFn = function(a, b) {
if (a[a.length - 1] < b[b.length - 1]) return -1;
if (a[a.length - 1] > b[b.length - 1]) return 1;
if (a[a.length - 1] == b[b.length - 1]) return 0;
}
myArray.sort(sortFn)
// sorts the array so that myArray = ["Wind","Fire","Rain"]
forEach
map()
filter()
every()
some(callback[, thisObject]) some(callback[, thisObject]) returns true if callback returns true for at least one item in the array.
Arrays can also be used like objects, to store related information.
const arr = [1, 2, 3]; arr.property = "value"; console.log(arr.property); // Logs "value"
Some JavaScript objects, such as the NodeList returned by document.getElementsByTagName() or the arguments object made available within the body of a function, look and behave like arrays on the surface but do not share all of their methods. The arguments object provides a length attribute but does not implement the forEach() method, for example.
Array methods cannot be called directly on array-like objects.
function printArguments() {
arguments.forEach(function(item) { // TypeError: arguments.forEach is not a function
console.log(item);
});
}
Copy to Clipboard
function printArguments() {
Array.prototype.forEach.call(arguments, function(item) {
console.log(item);
});
}
Copy to Clipboard
Array prototype methods can be used on strings as well, since they provide sequential access to their characters in a similar way to arrays:
Array.prototype.forEach.call('a string', function(chr) {
console.log(chr)
})
阅读量: 879
发布于:
修改于:
发布于:
修改于: