Created
April 9, 2023 03:31
-
-
Save DungGramer/c6b16cf90abb39956135be82df29e1c0 to your computer and use it in GitHub Desktop.
Remove ward when choose state, remove state when choose province
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let arr = [ | |
{ | |
name: 'P. PHÚC XÁ, Q. BA ĐÌNH, TP. HÀ NỘI', | |
provinceName: 'TP. HÀ NỘI', | |
provinceCode: '01', | |
stateName: 'Q. BA ĐÌNH', | |
stateCode: '001', | |
wardName: 'P. PHÚC XÁ', | |
wardCode: '00001', | |
}, | |
{ | |
name: 'P. PHÚC TÂN, Q. HOÀN KIẾM, TP. HÀ NỘI', | |
provinceName: 'TP. HÀ NỘI', | |
provinceCode: '01', | |
stateName: 'Q. HOÀN KIẾM', | |
stateCode: '002', | |
wardName: 'P. PHÚC TÂN', | |
wardCode: '00037', | |
}, | |
{ | |
name: 'Q. BA ĐÌNH, TP. HÀ NỘI', | |
provinceName: 'TP. HÀ NỘI', | |
provinceCode: '01', | |
stateName: 'Q. BA ĐÌNH', | |
stateCode: '001', | |
}, | |
{ | |
name: 'Q. BA ĐÌNH, TP. HÀ NỘI', | |
provinceName: 'TP. HÀ NỘI', | |
provinceCode: '01', | |
stateName: 'Q. BA ĐÌNH', | |
stateCode: '001', | |
}, | |
{ | |
name: 'Q. HOÀN KIẾM, TP. HÀ NỘI', | |
provinceName: 'TP. HÀ NỘI', | |
provinceCode: '01', | |
stateName: 'Q. HOÀN KIẾM', | |
stateCode: '002', | |
}, | |
{ | |
name: 'P. NHẬT TÂN, Q. TÂY HỒ, TP. HÀ NỘI', | |
provinceName: 'TP. HÀ NỘI', | |
provinceCode: '01', | |
stateName: 'Q. TÂY HỒ', | |
stateCode: '003', | |
wardName: 'P. NHẬT TÂN', | |
wardCode: '00094', | |
}, | |
{ | |
name: 'P. QUẢNG AN, Q. TÂY HỒ, TP. HÀ NỘI', | |
provinceName: 'TP. HÀ NỘI', | |
provinceCode: '01', | |
stateName: 'Q. TÂY HỒ', | |
stateCode: '003', | |
wardName: 'P. QUẢNG AN', | |
wardCode: '00100', | |
}, | |
{ | |
name: 'H. ĐỒNG VĂN, T. HÀ GIANG', | |
provinceName: 'T. HÀ GIANG', | |
provinceCode: '02', | |
stateName: 'H. ĐỒNG VĂN', | |
stateCode: '026', | |
}, | |
{ | |
name: 'H. MÈO VẠC, T. HÀ GIANG', | |
provinceName: 'T. HÀ GIANG', | |
provinceCode: '02', | |
stateName: 'H. MÈO VẠC', | |
stateCode: '027', | |
}, | |
{ | |
name: 'P. NHẬT TÂN, Q. TÂY HỒ, TP. HÀ NỘI', | |
provinceName: 'TP. HÀ NỘI', | |
provinceCode: '01', | |
stateName: 'Q. TÂY HỒ', | |
stateCode: '003', | |
wardName: 'P. NHẬT TÂN', | |
wardCode: '00094', | |
}, | |
{ | |
name: 'T. HÀ GIANG', | |
provinceName: 'T. HÀ GIANG', | |
provinceCode: '02', | |
}, | |
{ | |
name: 'T. HÀ NỘI', | |
provinceName: 'T. HÀ NỘI', | |
provinceCode: '01', | |
}, | |
{ | |
name: 'P. PHÚC XÁ, Q. BA ĐÌNH, TP. HÀ NỘI', | |
provinceName: 'TP. HÀ NỘI', | |
provinceCode: '01', | |
stateName: 'Q. BA ĐÌNH', | |
stateCode: '001', | |
wardName: 'P. PHÚC XÁ', | |
wardCode: '00001', | |
}, | |
]; | |
getUnique = (arr) => { | |
let payload = []; | |
arr = arr.filter( | |
(item, index) => | |
arr.findIndex( | |
(x) => | |
`${x.provinceCode}${x?.stateCode + ''}${x?.wardCode + ''}` === | |
`${item.provinceCode}${item?.stateCode + ''}${item?.wardCode + ''}` | |
) === index | |
); | |
let temp = [...arr]; | |
for (const item of arr) { | |
if (item.stateCode && !item.wardCode) { | |
temp = temp.filter((x) => x.stateCode !== item.stateCode); | |
temp.push(item); | |
} | |
} | |
payload = [...temp]; | |
temp = [...payload]; | |
for (const item of payload) { | |
if (item.provinceCode && !item.stateCode) { | |
temp = temp.filter((x) => x.provinceCode !== item.provinceCode); | |
temp.push(item); | |
} | |
} | |
payload = [...temp]; | |
return payload; | |
}; | |
console.log('result: ', getUnique(arr)); //? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment