this.accountList.unshift(assetList[0] || {})什么意思
时间: 2024-03-30 07:39:35 浏览: 21
这行代码是 JavaScript 代码,它的意思是将一个名为 `assetList` 的数组的第一个元素(如果存在)插入到名为 `accountList` 数组的开头。如果 `assetList` 的第一个元素不存在,就插入一个空对象 `{}` 到 `accountList` 数组的开头。`unshift()` 方法可在数组的开头添加一个或多个元素,同时返回新的数组长度。
相关问题
优化这段代码 search() { if (this.inputValue == '') { uni.showToast({ title: '搜索内容不能为空', icon: 'none' }); } else { let params = { paramsName: this.inputValue } // 商品查询 this.$u.api.findFoodstuffList(params).then(res => { if (res.list.length > 0) { this.goodsList = res.list } else { this.goodsList = [] } }) this.$u.api.findComboList(params).then(res => { if (res.list.length > 0) { this.comboList = res.list } else { this.comboList = [] } }) // 拼接(全部) this.dataList = this.goodsList.concat(this.comboList); if(this.dataList.length==""){ uni.showToast({ icon: 'none', title: '没有搜到哟,换个词试试' }) } // 搜索词列表 if (!this.searchHistoryList.includes(this.inputValue)) { this.searchHistoryList.unshift(this.inputValue);//给数组的开头添加元素, uni.setStorage({ key: 'searchList', data: JSON.stringify(this.searchHistoryList) }); } else { //有搜索记录,删除之前的旧记录,将新搜索值重新push到数组首位 let i = this.searchHistoryList.indexOf(this.inputValue); this.searchHistoryList.splice(i, 1); this.searchHistoryList.unshift(this.inputValue); uni.setStorage({ key: 'searchList', data: JSON.stringify(this.searchHistoryList) }); } } },
为了优化这段代码,你可以使用async/await方法改写它,如下所示:
async search() {
if (this.inputValue == '') {
uni.showToast({ title: '搜索内容不能为空', icon: 'none' });
} else {
let params = { paramsName: this.inputValue };
try {
const res1 = await this.$u.api.findFoodstuffList(params);
this.goodsList = res1.list.length > 0 ? res1.list : [];
const res2 = await this.$u.api.findComboList(params);
this.comboList = res2.list.length > 0 ? res2.list : [];
this.dataList = this.goodsList.concat(this.comboList);
if (this.dataList.length == "") {
uni.showToast({ icon: 'none', title: '没有搜到哟,换个词试试' });
}
if (!this.searchHistoryList.includes(this.inputValue)) {
this.searchHistoryList.unshift(this.inputValue);
await uni.setStorage({ key: 'searchList', data: JSON.stringify(this.searchHistoryList) });
} else {
let i = this.searchHistoryList.indexOf(this.inputValue);
this.searchHistoryList.splice(i, 1);
this.searchHistoryList.unshift(this.inputValue);
await uni.setStorage({ key: 'searchList', data: JSON.stringify(this.searchHistoryList) });
}
} catch (error) {
console.log(error);
}
}
}
该方法可以使代码更加简洁易懂,同时也可以减少代码中的回调地狱。
mounted() { this.cityArr = cityJson.data.list if (this.cityArr && this.cityArr[0]) { this.cityArr.map(v => { v.nameType = v.pinyin.substr(0, 1) }) this.cityList = this.groupArr(this.cityArr, 'nameType') } this.recordList = cityJson.data.recordList this.recordList.unshift({citycode:0,name:'全国'}) this.alphabet = cityJson.data.alphabet this.getLocation() }
这段代码看起来是一个 Vue.js 组件中的 mounted 钩子函数。在组件挂载后,它首先从一个 cityJson 数据源中获取城市列表,然后对城市列表进行了一些处理,比如给每个城市添加了一个 nameType 属性(表示城市名称的拼音首字母),并且将城市列表按照拼音首字母分组。接着,它又从 cityJson 数据源中获取了一个 recordList 数组,将一个包含全国城市的对象插入到数组开头,并将数组存储在组件的 recordList 属性中。最后,它还获取了一个字母表,并将其存储在组件的 alphabet 属性中。最后,它调用了 getLocation() 方法,可能是为了获取用户的地理位置信息。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)