知识点梳理

66 阅读3分钟

1.如何让[{},{}]+[{}]多个数组对象合并为一个数组[{}{}{}]

```let list = []
arr=[[{},{}],[{}]]
    arr.forEach(e => {
        Array.isArray(e) && list.push(...e);
        !Array.isArray(e) && list.push(e);
    });
    console.log(list)
### ***Array.isArray()用于确定传递的值是否是一个数组
```js

2.表格输入框修改实时禁止

```1.设置一个失去焦点事件@blur,定义一个属性值isBlur,给当前输入框一个禁止属性判断:disabled=isBlur&&isUpdate
   2.判断blur事件给isBlur布尔值,从而来实现编辑是实时禁止
   3.再打开弹窗时,先将blur设置为false。然后判断在编辑状态下根据不同的逻辑来给blur赋值(isBlur=isBlur=='条件'?true:false)

```js

3.给下拉选择框给个全部选项,选中则不能选其他值,并获得所有的数据,不选中则是选当个属性和属性值

```<a-form>
    <a-form-item label="属性名" v-bind="校验">
        <a-select placeholder="提示语" mode="multiple" v-model:value="绑定的值" option="option">
            <a-select-option v-model="(item,index) in list" :key="index" :value="item.id">
                {{item.name}}
            </a-select-option>
        </a-select> 
    </a-form-item>
<a-form>
### ***mode="multiple"--多选   mode="tag"--多选+可输入
```js

4.兄弟组件间传值

image.png

5.替换对象数组内的对象属性名

``````JSON.parse(JSON.stringify(arrayData).replace(/oldName/g, 'newName'))
//arrayData为数组,oldName为json内现有的,newName为想要更改的名字

6.对象里面去除某项

```对象去除某项
const obj = {  
    name: 'detanx',  
    age: 24,  
    height: 180  
}  
// 剔除height  
const { height, ...otherObj } = obj;  
console.log(otherObj) // {name: "detanx", age: 24}

7.实时更新子组件数据

```1.传给子组件的数据里面添加一个keytime对象名,赋值为new Date()
   2.给子组件动态绑定key值为这个对象
   current.value.keytime= new Date()
   子组件:<son :key="current.keytime"  :data="current"></son>

8数据转为树形结构

```/* 数据转为树形结构 */
const parseData = (data) => {
  let tree = toTreeData({
    data: data.map((d) => {
      return { ...d, key: d.id, value: d.id };
    }),
    idField: 'id',
    parentIdField: 'parentId'
  });
  return tree
};

9.数组对象里面某项去重

```//去重
let arrList = (arr) => {
    let obj = {}
    arr = arr.reduce((newArr, next) => {
        obj[next.userId] ? "" : (obj[next.userId] = true && newArr.push(next))
        return newArr
    }, [])
    return arr
}
****userId为去重的判断对象

10.数组使用的一些技巧

1.使用扩展运算符进行去重
**数组去重
let a = [1,2,3], let b = [1,3,4]
let c = [...new Set([...a,...b])]
**对象去重
let obj = {...obj1,...obj2} //{a:1,b:2}
2.合并数组/对象
**数组
let a = [1,2,3],
let b = [1,3,4]
let c = a.concat(b)   //[1, 2, 3, 1, 2, 4]
**对象
const obj1 = {a:1}
const obj2 = {b:2}
const obj = Object.assign({},obj2,obj1). //{a:1,b:2}

3.数据扁平化
let deps = {
	'采购部':[1,2,3],
	'人事部':[5,8,12],
	'行政部':[5,14,79],
	'运输部':[3,64,105]
}
let member = [];
for (let item in deps){
    const value = deps[item];
    if(Array.isArray(value)){
        member = [...member,...value]
    }
}
member = [...new Set(member)]   // [1, 2, 3, 5, 8, 12, 5, 14, 79, 3, 64, 105]

使用Object对象的values(),返回对象所有value值的组成的数组,es6的flat(),使用Infinity参数,无需知道被扁平化的数组的维度,就可以完成扁平化
member = Object.values(deps).flat(Infinity)

11.解决异步函数

```1.嵌套函数调用容易形成回调地狱,使用asycn/await
const fn = async () =>{ 
    const res1 = await fn1(); 
    const res2 = await fn2(); 
    console.log(res1);// 1 
    console.log(res2);// 2 
    }
    2.需要用到并发请求时,使用Promise.all()
 const fn = () =>{ 
     Promise.all([fn1(),fn2()]).then(res =>{ 
     console.log(res);// [1,2] }) 
     }

foreach 本身是同步的,但如果 foreach 中加了 async 和 await,就变相让这个 foreach 变成了异步。

如果要在循环中处理 async 和 await 方法,直接使用 for 就好了