【问题标题】:Change structure of JavaScript array [duplicate]更改JavaScript数组的结构[重复]
【发布时间】:2022-01-13 02:44:37
【问题描述】:

我是 javascript 的初学者,我有一个小问题。 我想使用部分 List 更改数组的结构以在 React Native 中呈现,

我从 Web Api 得到这个 JSON

[
  {
    title: "Test",
    c: 1,
    d: 2,
  },
  {
    title: "Test",
    c: 3,
    d: 4,
  },
  {
    title: "Test",
    c: 5,
    d: 6,
  },
  {
    title: "Test01",
    c: 1,
    d: 2,
  },
  {
    title: "Test01",
    c: 3,
    d: 4,
  },
  {
    title: "Test01",
    c: 5,
    d: 6,
  },
  {
    title: "Test02",
    c: 1,
    d: 2,
  },
  {
    title: "Test02",
    c: 3,
    d: 4,
  },
  {
    title: "Test02",
    c: 5,
    d: 6,
  },
];

我想像这样更改这个 JSON

[
  {
    title: "Test",
    data: [
      { c: 1, d: 2 },
      { c: 3, d: 4 },
      { c: 5, d: 6 },
    ],
  },
  {
    title: "Test01",
    data: [
      { c: 1, d: 2 },
      { c: 3, d: 4 },
      { c: 5, d: 6 },
    ],
  },
  {
    title: "Test02",
    data: [
      { c: 1, d: 2 },
      { c: 3, d: 4 },
      { c: 5, d: 6 },
    ],
  },
];

【问题讨论】:

  • 解决我的问题非常有用,但它不能回答我的问题,我需要和我上面提到的一样,如果我得到这样的话,只有我可以使用部分列表呈现。谢谢哥们..!快速响应
  • 由于问题已关闭检查我的solution

标签: javascript arrays json


【解决方案1】:

将数据键入测试名称会更简单,但您可以通过像这样映射数组来实现您想要的:

let new_array=[];
your_array.forEach(elem => {
  let title = elem.title;
  let matchingIndex = newArray.findIndex(a => a.title = title);
  if (matchingIndex === -1) {
    new_array.push({title}
    matchingIndex = new_array.length - 1;
  }
let dataColumns = ['c', 'd'];
let data = {};
dataColumns.forEach(col => {
 data[col] = elem[col];
});
if (!Array.isArray(new_array[matching_index].data)) {
  isArray(new_array[matching_index].data = [];
}
new_array[matching_index].data.push(data);
});

【讨论】:

    【解决方案2】:

    可以对数组进行reduce操作,得到想要的格式。

    const items = [
      {
        title: "Test",
        c: 1,
        d: 2,
      },
      {
        title: "Test",
        c: 3,
        d: 4,
      },
      {
        title: "Test",
        c: 5,
        d: 6,
      },
      {
        title: "Test01",
        c: 1,
        d: 2,
      },
      {
        title: "Test01",
        c: 3,
        d: 4,
      },
      {
        title: "Test01",
        c: 5,
        d: 6,
      },
      {
        title: "Test02",
        c: 1,
        d: 2,
      },
      {
        title: "Test02",
        c: 3,
        d: 4,
      },
      {
        title: "Test02",
        c: 5,
        d: 6,
      },
    ];
    
    const formatted = items.reduce((carry, current) => {
        // generating the placeholder format to put the data 
        if(!carry.hasOwnProperty(current.title)) {
          carry[current.title] = {
            title: current.title,
            data: []
          };
        }
        // Setting the data in unique name
        carry[current.title].data.push({ c: current.c, d: current.d });
        return carry;
    }, []);
    // formatted will have key value pair
    console.log(Object.values(formatted));

    【讨论】:

      【解决方案3】:

      将结果累加到数组中的reduce函数:

      const raw = [{
          title: 'Test',
          c: 1,
          d: 2,
        },
        {
          title: 'Test',
          c: 3,
          d: 4,
        },
        {
          title: 'Test',
          c: 5,
          d: 6,
        },
        {
          title: 'Test01',
          c: 1,
          d: 2,
        },
        {
          title: 'Test01',
          c: 3,
          d: 4,
        },
        {
          title: 'Test01',
          c: 5,
          d: 6,
        },
        {
          title: 'Test02',
          c: 1,
          d: 2,
        },
        {
          title: 'Test02',
          c: 3,
          d: 4,
        },
        {
          title: 'Test02',
          c: 5,
          d: 6,
        },
      ];
      
      const result = raw.reduce((acc, {
        title,
        ...data
      }) => {
        const index = acc.findIndex((elem) => title === elem.title);
        if (index === -1) acc.push({
          title,
          data: [data]
        });
        else acc[index].data.push(data);
        return acc;
      }, []);
      console.log(result);

      【讨论】:

        【解决方案4】:

        const input =[
          {
            title: "Test",
            c: 1,
            d: 2,
          },
          {
            title: "Test",
            c: 3,
            d: 4,
          },
          {
            title: "Test",
            c: 5,
            d: 6,
          },
          {
            title: "Test01",
            c: 1,
            d: 2,
          },
          {
            title: "Test01",
            c: 3,
            d: 4,
          },
          {
            title: "Test01",
            c: 5,
            d: 6,
          },
          {
            title: "Test02",
            c: 1,
            d: 2,
          },
          {
            title: "Test02",
            c: 3,
            d: 4,
          },
          {
            title: "Test02",
            c: 5,
            d: 6,
          },
        ];
        let result = {};
        input.forEach((e)=>{
          if(!result[e.title]){
          result[e.title] = {data:[]};
        }
          result[e.title]['data'].push({ c:e.c, d:e.d});
        });
        let restructured =[];
        Object.keys(result).forEach((key)=>{
          restructured.push({
            title: key, data:result[key].data
        })
        });
        console.log(restructured)

        【讨论】:

          【解决方案5】:
          var data = [
              {
                title: "Test",
                c: 1,
                d: 2,
              },
              {
                title: "Test",
                c: 3,
                d: 4,
              },
              {
                title: "Test",
                c: 5,
                d: 6,
              },
              {
                title: "Test01",
                c: 1,
                d: 2,
              },
              {
                title: "Test01",
                c: 3,
                d: 4,
              },
              {
                title: "Test01",
                c: 5,
                d: 6,
              },
              {
                title: "Test02",
                c: 1,
                d: 2,
              },
              {
                title: "Test02",
                c: 3,
                d: 4,
              },
              {
                title: "Test02",
                c: 5,
                d: 6,
              },
            ];
          console.log('data',data)
          
          function analyse(data){
              var tmp = {}
              for(let item of data){
                if(tmp[item.title]){
                    tmp[item.title].data.push({c:item.c,d:item.d})
                }else{
                    tmp[item.title] ={
                        data: [{c:item.c,d:item.d}]
                    }
            
                }
              }
              console.log('tmp',tmp)
            var results = []
              for(let key in tmp){
                results.push({title:key,data:tmp[key].data})
              }
              console.log('results',JSON.stringify(results))
            return results
          }
          
          analyse(data)
          

          【讨论】:

            猜你喜欢
            • 2021-12-27
            • 1970-01-01
            • 2011-11-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-07-31
            • 2019-02-19
            相关资源
            最近更新 更多