今日份略有成就感的小技巧
2020-08-05 本文已影响0人
Gifted_
//一个数组,通过某一字段(area_name )合并数组
方法1
let arr = []
json.forEach(item => {
arr.push(item.area_name)
})
let arr2 = [...new Set(arr)]
arr2.forEach((item, index) => {
let Temporary = json.filter(it => {
return it.area_name == item
})
arr2[index] = Temporary
})
console.log(arr2)
方法2
let arr = []
for (let elem of json.values()) {
if (!arr.includes(elem.area_name)) {
arr.push(elem.area_name)
}
}
console.log(arr)
arr.forEach((item, index) => {
let Temporary = json.filter(it => {
return it.area_name == item
})
arr[index] = Temporary
})
console.log(arr)