在数组中添加对象,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
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)
var arr = ['1','2','', '3.jpg','']
var newArr = arr.filter(item => item)
console.log(newArr)
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)
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);
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); //此处数组根据id去重可参考我上一篇文章,这里就不细说了。
正则替换数组中某个字段名
let treeData = JSON.parse(JSON.stringify(data).replace(/list/g, 'children'))
给树形(层级)数组添加层级标识
const arrayTreeAddLevel = (array, levelName = 'level', childrenName = 'children') => {
if (!Array.isArray(array)) return []
const recursive = (array, level = 0) => {
console.log(array)
level++
return array.map(v => {
v[levelName] = level
const child = v[childrenName]
if (child && child.length) recursive(child, level)
return v
})
}
return recursive(array)
}