// 方式1
var fruits1 = ['banana', 'apple', 'orange', 'watermelon', 'apple', 'orange', 'grape', 'apple'];
var uniqueFruits1 = Array.from(new Set(fruits1))
console.log(uniqueFruits1)//["banana", "apple", "orange", "watermelon", "grape"]
// 方式2
var uniqueFruits2 = [...new Set(fruits1)]
console.log(uniqueFruits2)// ["banana", "apple", "orange", "watermelon", "grape"]
// 方式3
var uniqueFruits3 = [];
fruits1.forEach(element => {
if(uniqueFruits3.indexOf(element) === -1) {
uniqueFruits3.push(element)
}
});
console.log(uniqueFruits3)
// 方式4
var uniqueFruits4 = [];
fruits1.forEach((element) => {
if(!uniqueFruits4.includes(element)){
uniqueFruits4.push(element)
}
})
console.log(uniqueFruits4)
var fruits2 = ['banana', 'apple', 'orange', 'watermelon', 'apple', 'orange', 'grape', 'apple'];
fruits2.splice(0, 2, 'pitaya', 'mango')
console.log(fruits2)// ["pitaya", "mango", "orange", "watermelon", "apple", "orange", "grape", "apple"]
var friends1 = [
{name: 'John', age: 22},
{name: 'Peter', age: 23},
{name: 'Mark', age: 24},
{name: 'Maria', age: 25},
{name: 'Monica', age: 26},
{name: 'Martha', age: 27}
];
// 方式1
var friendName1 = friends1.map((item) => {
return item.name
})
console.log(friendName1)// ["John", "Peter", "Mark", "Maria", "Monica", "Martha"]
// 方式2
var friendName2 = Array.from(friends1, ({name}) => name)
console.log(friendName2)// ["John", "Peter", "Mark", "Maria", "Monica", "Martha"]
var fruits3 = ['banana', 'apple', 'orange', 'watermelon', 'apple', 'orange', 'grape', 'apple'];
fruits3.length = 0
console.log(fruits3)// []
var fruits4 = ['banana', 'apple', 'orange', 'watermelon', 'apple', 'orange', 'grape', 'apple'];
var fruits4Obj = {...fruits4}
console.log(fruits4Obj)// {0: "banana", 1: "apple", 2: "orange", 3: "watermelon", 4: "apple", 5: "orange", 6: "grape", 7: "apple"}
var newArray = new Array(10).fill('520')
console.log(newArray)// ["520", "520", "520", "520", "520", "520", "520", "520", "520", "520"]
var fruit5 = ['banana', 'apple', 'orange', 'watermelon', 'grape'];
var meat = ['poultry', 'beef', 'fish'];
var vegetable = ['potato', 'tomato', 'cucumber'];
var concat1 = fruit5.concat(meat)
console.log(concat1)// ["banana", "apple", "orange", "watermelon", "grape", "poultry", "beef", "fish"]
// 或
var food = [...fruit5, ...meat, ...vegetable];
console.log(food)// ["banana", "apple", "orange", "watermelon", "grape", "poultry", "beef", "fish", "potato", "tomato", "cucumber"]
var numOne = [0,1,2,4,6,8,8];
var numTwo = [1,2,3,8,8];
var duplicatedValues = [...new Set(numOne)].filter(item=> numTwo.includes(item))
console.log(duplicatedValues)// [1, 2, 8]
备注:Array.prototype.includes()
// includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。
let arr = [20,29,44,-4,52,205]
console.log(arr.includes(20,1))// false
console.log(arr.includes(20,0)) // true
console.log(arr.includes(20)) // true
var mixedArr = [0, NaN, 9, false, undefined, '', 'black']
var trueArr = mixedArr.filter(Boolean)
// 等价于array.filter((item) => {return Boolean(item)})
console.log(trueArr)// [9, "black"]
var fruit6 = ['banana', 'apple', 'orange', 'watermelon', 'grape'];
var fruit6random = fruit6[Math.floor(Math.random()* (fruit6.length))]
console.log(fruit6random)// grape 或者是数组里的其他项
var fruit7 = ['banana', 'apple', 'orange', 'watermelon', 'grape'];
console.log('反转前', fruit7);// 反转前 (5) ["banana", "apple", "orange", "watermelon", "grape"]
var reversedFruit7 = fruit7.reverse();
console.log('反转后', reversedFruit7);// 反转后 (5) ["grape", "watermelon", "orange", "apple", "banana"]
var nums1 = [1,2,3,4,5,6,7,8,9,5,10];
var lastIndex = nums1.lastIndexOf(5);
console.log(lastIndex)// 9
var nums2 = [1,2,3,4,5,6,7,8,9,10];
var sum = nums2.reduce((a, b)=>{
return a + b
})
console.log(sum)// 55
let indices = []
let arr = ['a', 'b', 'a', 'c', 'a', 'd']
let element = 'a'
let idx = arr.indexOf(element)
while(idx != -1){
// 如果找到元素则把它的索引推进数组
indices.push(idx)
idx = arr.indexOf(element, idx + 1);
}
console.log(indices)// [0, 2, 4]
未完待续…