1.for循環(huán)與forEach便瑟,比較簡單的操作就不貼代碼了缆毁,唯一不同的在于for循環(huán)內(nèi)部相應break等中斷操作,forEach不支持到涂,
2.for...of脊框,基于es6的新方法,完美支持break等中斷循環(huán)操作践啄,
for (let item of CityDataJson) {
if (nameId == item.id) {
for (let cityList of item.city) {
if (cityId == cityList.id) {
for (let areaList of cityList.area) {
if (areaId == areaList.id) {
this.setState({
provinceId: CityDataJson[preventIndex].id,
provinceDesc: CityDataJson[preventIndex].name,
cityId: cityList[cityIndex].id,
cityDesc: cityList[cityIndex].name,
areaId: areaList[areaIndex].id,
areaDesc: areaList[areaIndex].name
});
break;
}
}
break;
}
}
break;
}
}
3.基于lodash的findIndex方法浇雹,快速找到對應數(shù)據(jù)下標,代碼如下往核;
let preventIndex = findIndex(CityDataJson, ['id', nameId]),
cityList = CityDataJson[preventIndex].city,
cityIndex = findIndex(cityList, ['id', cityId]),
areaList = cityList[cityIndex].area,
areaIndex = findIndex(areaList, ['id', areaId]);
console.log(cityIndex, cityId);
this.setState({
provinceId: CityDataJson[preventIndex].id,
provinceDesc: CityDataJson[preventIndex].name,
cityId: cityList[cityIndex].id,
cityDesc: cityList[cityIndex].name,
areaId: areaList[areaIndex].id,
areaDesc: areaList[areaIndex].name
});