计算数组中所有值的总和
var numbers = [3,5,9];
var sumValue = numbers.reduce(function(sum,number){ //sum2 前两个数的和
console.log(sum) //1000 1003 1008
return sum + number;
},1000 ) //第一次初始化时用1000 + 数组中的第一项
console.log(sumValue) //1017
将数组中对象的某个属性抽离到另一个数组中
var primaryColors = [
{color:"red"},
{color:"yellow"},
{color:"bule"}
]
var colorsArr = primaryColors.reduce(function(arr,primaryColor){
arr.push(primaryColor.color);
return arr;
},[]);
console.log(colorsArr) //["red", "yellow", "bule"]
在数组中添加对象,push()
let arr = [{a:1}]
let obj = {
b:2
}
arr.push(obj)
数组转换成字符串,并且用指定符合分割,join()
var joinArr = ["hello","world"];
var joinArr1 = joinArr.join(","); // joinArr1: "hello,world"
###字符串转换成数组,split()
var str = "hello,world"
var arr = str.split(",") //此处有个坑,如果str没有值,arr也会加个,变成脏数据,所以我一般加个三元运算符
var arr = str ? str.split(",") : []
数组对象转换成字符串,JSON.stringify
var data = JSON.stringify(data)
字符串转换成数组对象,JSON.parse
var data = JSON.parse(data)
合并对象,Object.assign
const target = { a: 1, b: 1 };
const source1 = { b: 2, c: 2 };
const source2 = { c: 3 };
Object.assign(target, source1, source2);
target // {a:1, b:2, c:3} //如果对象有同名属性,后面的属性会覆盖前面的属性
过滤数组,arr.filter
// 1、去掉空数组空字符串、undefined、null
var arr = ['1','2',undefined, '3.jpg',undefined]
var newArr = arr.filter(item => item)
console.log(newArr)
var arr = ['1','2',null, '3.jpg',null]
var newArr = arr.filter(item => item)
console.log(newArr)
// 2、空字符串里面不能包含空格
var arr = ['1','2','', '3.jpg','']
var newArr = arr.filter(item => item)
console.log(newArr)
// 3、去掉数组中不符合项
var arr = [20,30,50, 96,50]
var newArr = arr.filter(item => item>40)
console.log(newArr)
var arr = ['10','12','23','44','42']
var newArr = arr.filter(item => item.indexOf('2')<0)
console.log(newArr)
// 4、数组去重
var arr = [1, 2, 2, 3, 4, 5, 5, 6, 7, 7,8,8,0,8,6,3,4,56,2];
var arr2 = arr.filter((x, index,self)=>self.indexOf(x)===index)
console.log(arr2); //[1, 2, 3, 4, 5, 6, 7, 8, 0, 56]
// 5、两个数组取相同项
let arr = [{ id: 1, name: 'zs' }, { id: 2, name: 'ls' }, { id: 3, name: 'ww' }, { id: 4, name: 'xm' }, { id: 5, name: 'xh' },]
let sum = [3, 4, 5]
let newArr = arr.filter(item => sum.indexOf(item.id) > -1)
console.log(newArr)
合并数组
var arr1 = [1, 2, 3];
var arr2 = ["a","b","c","d","e","f"];
// 1、concat方法
var arr = arr1.concat(arr2); // [1, 2, 3, "a", "b", "c", "d", "e", "f"]
// 2、 通过for循环
for(let i in arr1){
arr2.push(arr[i]);
}
console.log(arr2) // [1, 2, 3, "a", "b", "c", "d", "e", "f"]
// 3、通过map()
arr1.map(item=>{
arr2.push(item)
});
console.log(arr2) // [1, 2, 3, "a", "b", "c", "d", "e", "f"]
// 4、apply
arr1.push.apply(arr1,arr2);
console.log(arr1) // [1, 2, 3, "a", "b", "c", "d", "e", "f"]
// 5、ES6 – 扩展运算符
arr = [...arr1,...arr2]
console.log(arr) // [1, 2, 3, "a", "b", "c", "d", "e", "f"]
合并数组去重
let arr1 = [1, 1, 2, 3, 6, 9, 5, 5, 4]
let arr2 = [1, 2, 5, 4, 9, 7, 7, 8, 8]
function uniqueArr(arr1,arr2) {
//合并两个数组
arr1.push(...arr2)//或者arr1 = [...arr1,...arr2]
//去重
let arr3 = Array.from(new Set(arr1))//let arr3 = [...new Set(arr1)]
console.log(arr3)
}
uniqueArr(arr1,arr2)
合并数组,并根据对象中id的值去重
//数组1
let arr1 = [
{
id: 1,
name: "张三",
age: 18
},
{
id: 2,
name: "李四",
age: 18
},
{
id: 3,
name: "王五",
age: 18
}
];
// 数组2
let arr2 = [
{
id: 1,
name: "张三"
},
{
id: 2,
name: "李四"
}
];
arr1.push(...arr2); //合并数组
let hash = {}; //去重
let arr = arr1.reduce((preVal, curVal) => {
hash[curVal.id] //id就是数组中的id字段
? ""
: (hash[curVal.id] = true && preVal.push(curVal));
return preVal;
}, []);
console.log(arr);
原文链接:https://blog.csdn.net/weixin_41698051/article/details/103602327
文档更新时间: 2022-08-02 15:36 作者:admin