37 lines
1.0 KiB
JavaScript
37 lines
1.0 KiB
JavaScript
array = ["Bilbo", "Gandalf", "Nazgul"];
|
|
array.forEach((item, index, array) =>{
|
|
console.log(`${item} is at index ${index} in ${array}`)
|
|
});
|
|
|
|
console.log(`arr.indexOf(Bilbo): ${array.indexOf("Bilbo")}`)
|
|
console.log(`arr.indexOf(Bilbo): ${array.includes("Bilbo")}`)
|
|
|
|
let fruits = ['Apple', 'Orange', 'Apple']
|
|
console.log(`fruits.indexOf('Apple'): ${fruits.indexOf('Apple')}`)
|
|
console.log(`fruits.lastIndexOf('Apple'): ${fruits.lastIndexOf('Apple')}`)
|
|
|
|
// arr.find(fn)
|
|
let users = [
|
|
{id: 1, name: "xingzhesun"},
|
|
{id: 2, name: "zhexingsun"},
|
|
{id: 3, name: "sunxingzhe"},
|
|
];
|
|
|
|
let user1 = users.find(item => item.id == 1);
|
|
let user2 = users.findLastIndex(item => item.id == 1);
|
|
console.log(user1.name);
|
|
console.log(user2.name);
|
|
|
|
// arr.filter
|
|
let someUsers = users.filter(user => user.id < 3);
|
|
console.log(someUsers)
|
|
someUsers = users.filter(function(item, index, array){
|
|
if (item.id < 3){
|
|
return true;
|
|
}
|
|
})
|
|
console.log(someUsers);
|
|
|
|
// arr.map(fn)
|
|
let lengths = array.map(item=>item.length);
|
|
alert(lengths); |