【问题标题】:How to create new array from existing array with different structure如何从具有不同结构的现有数组创建新数组
【发布时间】:2022-01-27 05:53:32
【问题描述】:

我想重新格式化现有数组,如何使用 Javascript 或 typescript 来完成

第一个数组

pdfData: [
         { 
           Id: 1, 
           path:'https//path/pdf1'
         },
         { 
           Id: 1, 
           path:'https//path/pdf2'
         },
         { 
           Id: 2, 
           path:'https//path/pdf1'
         },
         { 
           Id: 2, 
           path:'https//path/pdf2'
         },
        ]

我想把这个数组转换成下面的一个

data: [
        { 
           Id: 1, 
           links:['https//path/pdf1','https//path/pdf2']
         },
         { 
           Id: 2, 
           links:['https//path/pdf1','https//path/pdf2']
         },

  ]

【问题讨论】:

  • 这能回答你的问题吗? JavaScript group objects
  • 您应该先尝试一下,以便我们知道您遇到困难的部分。否则,我们只是为您完成您的工作。

标签: javascript arrays typescript


【解决方案1】:

应该这样做:

const pdfData = [
    {
        Id: 1,
        path: 'https//path/pdf1'
    },
    {
        Id: 1,
        path: 'https//path/pdf2'
    },
    {
        Id: 2,
        path: 'https//path/pdf1'
    },
    {
        Id: 2,
        path: 'https//path/pdf2'
    },
]



const result = [];

for (const item of pdfData) {
    const existing = result.find(it => it.Id === item.Id);
    if (existing) {
        existing.links.push(item.path)
    } else {
        result.push({
            Id: item.Id,
            links: [item.path]
        })
    }
}

【讨论】:

    【解决方案2】:

    您询问的是 Typescript 或 Javascript - 这是 Typescript 版本:

    // Typescript version
    const pdfData = [
             { 
               Id: 1, 
               path:'https//path/pdf1'
             },
             { 
               Id: 1, 
               path:'https//path/pdf2'
             },
             { 
               Id: 2, 
               path:'https//path/pdf1'
             },
             { 
               Id: 2, 
               path:'https//path/pdf2'
             },
            ]
    
    interface TransformedDataItem {
        Id: number,
        links: string[]
    }
    
    const result = [] as TransformedDataItem[];
    
    for (const item of pdfData) {
        const existing = result.find(it => it.Id === item.Id);    
        if (existing) {
            existing.links.push(item.path)
        } else {
            result.push({
                Id: item.Id,
                links: [item.path]
            })
        }
    }
    

    【讨论】:

      【解决方案3】:

      另一种,一种ES6风格的解决方案

      const data = [{Id: 1, path:'https//path/pdf1'},{ Id: 1, path:'https//path/pdf2'},{ Id: 2, path:'https//path/pdf1'},{ Id: 2, path:'https//path/pdf2'}];
      
      const result = Object.values(data.reduce((acc, { Id, path }) => ({
        ...acc, 
        [Id]: acc[Id] 
          ? { ...acc[Id], links: [...acc[Id].links, path] }
          : { Id, links: [path] },
      }), {}));
      
      console.log(result);
      .as-console-wrapper{min-height: 100%!important; top: 0}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-15
        • 1970-01-01
        • 2021-03-09
        • 2016-08-25
        • 1970-01-01
        • 2017-11-19
        相关资源
        最近更新 更多