1.for循环进行数组中对象去重和去null
//原数组 let objArr = [{ specialName: '名称1' },{ specialName: '名称2' },{ specialName: '名称3' },{ specialName: '名称1' },{ specialName: null }] let s = {} let newObjArr = [] for (let i = 0;i < objArr.length; i++) { console.log(objArr[i].specialName) if ((!s[objArr[i].specialName] )&& objArr[i].specialName!=null) { newObjArr.push(objArr[i]); s[objArr[i].specialName] = true } } console.log(newObjArr)
//输出结果:[{specialName: "名称1"},{specialName: "名称2"},{specialName: "名称3"}]
2.利用for循环并使用substring进行截取操作
let arr = [{ name: "测试", birthday: "2020-02-17", gender: 1, }, { name: "测试2", birthday: "2019-11-27", gender: 0, }] for (let i = 0; i < arr.length; i++) { console.log(arr[i]) arr[i].birthday = arr[i].birthday.substring(0, 4) //截取birthday前四位 } console.log(arr) //输出结果[{name: "测试", birthday: "2020", gender: 1},{name: "测试2", birthday: "2019", gender: 0}]