js数组与工具-good!

文章目录

          • 数组篇
            • 数组去重
            • 替换特定的值
            • 从对象映射出数组(map与Array.from)
            • 转成空数组
            • 数组转化为对象
            • 填充数组
            • 合并数组
            • 求数组交集
            • 用过滤从数组中删除假值,保留真值
            • 从数组中获取随机值
            • 数组反转
            • .lastindexOf用法,找到最后一次出现的索引
            • 所有值相加
            • 判断元素是否在数组里面

备注:以下东西都可以用 export const + 变量 导出去,然后用 require去引用!

数组篇
数组去重
// 方式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"]
从对象映射出数组(map与Array.from)
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"]
.lastindexOf用法,找到最后一次出现的索引
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]

未完待续…

你可能感兴趣的:(javascript)