typescript 如何将一个对象数组构造为唯一的对象数组

ih99xse1  于 2022-11-18  发布在  TypeScript
关注(0)|答案(3)|浏览(198)

我有一个这样对象数组

let data = 
[
    {
        text: 'label'
    },
    {
        text: 'username'
    },
    {
        text: 'category'
    },
    {
        text: 'book'
    },
    {
        text: 'john'
    },
    {
        text: 'education'
    },
    {
        text: 'car'
    },
    {
        text: 'doe'
    },
    {
        text: 'automotive'
    },
    {
        text: 'shoes'
    },
    {
        text: 'cena'
    },
    {
        text: 'fashion'
    },
]

以及我期望对象数组

let result = 
[
    {
        label: 'book',
        username: 'john',
        category: 'education'
    },
    {
        label: 'car',
        username: 'doe',
        category: 'automotive'
    },
    {
        label: 'shoes',
        username: 'cena',
        category: 'fashion'
    },
]
fd3cxomn

fd3cxomn1#

一个简单的for循环可能是最清楚的。这里将每个对象存储在一个temp变量中,以避免每次迭代都要访问结果数组的末尾,并将size抽象为一个变量。

let data = [{ text: 'label' }, { text: 'username' }, { text: 'category' }, { text: 'book' }, { text: 'john' }, { text: 'education' }, { text: 'car' }, { text: 'doe' }, { text: 'automotive' }, { text: 'shoes' }, { text: 'cena' }, { text: 'fashion' },];

const size = 3;
const result = [];

for (let temp, i = size; i < data.length; i++) {
  if (i % size === 0) {
    result.push(temp = {});
  }
  temp[data[i % size].text] = data[i].text;
}

console.log(result)
x6h2sr28

x6h2sr282#

使用switch-case和modulo %运算符检查当前密钥如何:

const transformData = (data) => {
  let result = [];
  let tmpObj = {};
  data.forEach((element, idx) => {
    switch (idx % 3) {
      case 0:
        tmpObj["label"] = element.text;
        break;
      case 1:
        tmpObj["username"] = element.text;
        break;
      case 2:
        result.push({ ...tmpObj,
          category: element.text
        });
        tmpObj = {};
        break;
      default:
        break;
    }
  });
  return result;
};

console.log(transformData(getSampleData()));

function getSampleData() {
  return [{
      text: 'label'
    },
    {
      text: 'username'
    },
    {
      text: 'category'
    },
    {
      text: 'book'
    },
    {
      text: 'john'
    },
    {
      text: 'education'
    },
    {
      text: 'car'
    },
    {
      text: 'doe'
    },
    {
      text: 'automotive'
    },
    {
      text: 'shoes'
    },
    {
      text: 'cena'
    },
    {
      text: 'fashion'
    },
  ];
}
ajsxfq5m

ajsxfq5m3#

根据您的数据,前3条记录是属性名称,其他记录是数据,因此我们可以使用Array.slice()来获取属性名称
然后,我们可以使用Array.reduce()转换左侧数据

let keys = data.slice(0,3).map(v => v.text)
let result = data.slice(3).reduce((a,c,i) =>{
  let key = keys[i%3]
  if(i%keys.length ==0){
   let obj = {}
   obj[key] = c.text
   a.push(obj)
  }else{
   a.at(-1)[key]=c.text
  }
  return a
},[])

console.log(result)
let data = 
[
    {
        text: 'label'
    },
    {
        text: 'username'
    },
    {
        text: 'category'
    },
    {
        text: 'book'
    },
    {
        text: 'john'
    },
    {
        text: 'education'
    },
    {
        text: 'car'
    },
    {
        text: 'doe'
    },
    {
        text: 'automotive'
    },
    {
        text: 'shoes'
    },
    {
        text: 'cena'
    },
    {
        text: 'fashion'
    },
]

let keys = Object.values(data.slice(0,3)).map(v => v.text)
let result = data.slice(3).reduce((a,c,i) =>{
  let key = keys[i%3]
  if(i%keys.length ==0){
   let obj = {}
   obj[key] = c.text
   a.push(obj)
  }else{
   a.at(-1)[key]=c.text
  }
  return a
},[])

console.log(result)

相关问题