vue遍历对象

47 阅读1分钟

转化对象

  const aa = {
        name: '123',
        age: 30
      }

      const arr = ['姓名', '年龄']

      const target = arr.reduce((accumulator, currentKey) => {
        // 寻找arr中每个元素在aa中的对应key,并赋值给accumulator
        const newKey = Object.keys(aa).find(key => key === currentKey)
        if (newKey) {
          accumulator[currentKey] = aa[newKey]
        }
        return accumulator
      }, {})

遍历对象

image.png