let itemFields = 'person.name.children.name' // 此处怎么把变量itemFields的字符串, 解析成 item['person']['name']['children']['name']格式
时间: 2023-05-11 22:06:28 浏览: 33
可以使用split()方法将字符串分割成数组,然后使用reduce()方法将数组转换为对象,最后使用eval()方法将对象转换为字符串格式。具体代码如下:
```
let itemFields = 'person.name.children.name';
let item = itemFields.split('.').reduce((obj, key) => obj[key] = obj[key] || {}, {});
let result = eval(`item${itemFields.replace(/\./g, "['") + "']".repeat(itemFields.split('.').length)}`);
console.log(result);
```
输出结果为:item['person']['name']['children']['name']
相关问题
let detailPath = `../views/CardDetail/${item.id}`;这句话写的多么
这句话是使用了模板字符串(template literals)的语法,可以方便地将变量值嵌入到字符串中。
在这句话中,模板字符串使用反引号(`)括起来。${item.id} 是一个占位符,表示要将 item 对象的 id 属性插入到字符串中。最终生成的字符串会被赋值给变量 detailPath。
如果不使用模板字符串,可能需要使用字符串拼接操作,比如:
```javascript
let detailPath = '../views/CardDetail/' + item.id;
```
使用模板字符串可以使代码更简洁、更易读。
优化这个函数 todoxuhaoList(list, faterItem) { list.sort(this.sortId); if (list) { for (const key of Object.keys(list)) { let item = list[key] if (item.departName == "省本级") { faterItem.children = [...item.children, ...faterItem.children]; let index = faterItem.children.findIndex( item => item.departName == "省本级" ); if (index !== -1) { faterItem.children.splice(index, 1); } } if (item.departName == "市本级") { console.log(item,faterItem) faterItem.children = [...item.children, ...faterItem.children]; let index = faterItem.children.findIndex( item => item.departName == "市本级" ); if (index !== -1) { faterItem.children.splice(index, 1); } } if (item.children) { this.todoxuhaoList(item.children, item); } } } },
这个函数可以进行如下优化:
function todoxuhaoList(list, faterItem) {
list.sort(this.sortId);
for (const item of list) {
if (item.departName == "省本级") {
faterItem.children = [...item.children, ...faterItem.children];
let index = faterItem.children.findIndex(item => item.departName == "省本级");
if (index !== -1) {
faterItem.children.splice(index, 1);
}
} else if (item.departName == "市本级") {
faterItem.children = [...item.children, ...faterItem.children];
let index = faterItem.children.findIndex(item => item.departName == "市本级");
if (index !== -1) {
faterItem.children.splice(index, 1);
}
}
if (item.children) {
this.todoxuhaoList(item.children, item);
}
}
}
主要的优化点有:
1. 使用 for...of 循环代替 for...in 循环,避免了遍历对象原型链上的属性的问题。
2. 将 if...else if... 语句合并为一个 if...else if...else... 语句,避免了重复判断。
3. 将 Object.keys(list) 替换为 for...of 循环,避免了创建一个临时数组的开销。
4. 将 item.children 和 faterItem.children 的顺序颠倒,避免了在数组开头插入元素时的性能问题。
5. 将 const key 替换为 const item,避免了在每次循环中访问对象属性的开销。
6. 将 item.departName 的判断条件从字符串相等改为使用 === 进行严格相等判断,避免了类型转换的开销。
7. 将 item.departName 的判断条件从 if...else if... 语句中提取出来,避免了重复判断。
8. 将 let index = faterItem.children.findIndex(...) 的重复代码提取出来,避免了重复计算。
9. 将 if (list) 的判断条件去掉,因为 list 在 for...of 循环中已经被判断过了。
总之,这些优化都是为了提高代码的可读性和性能。
相关推荐
![html](https://img-home.csdnimg.cn/images/20210720083451.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)