简体   繁体   English

如何从数组的键值对创建一个新数组?

[英]How to create a new array from array's key value pair?

I need an efficient way to convert this array:我需要一种有效的方法来转换这个数组:

[
    {
      uuid: 1,
      date: '2020-12-25',
      note: 'example note 1',
      time: '12:05:00',
      language: ['english']
    },
    {
      uuid: 2,
      date: '2020-12-25',
      note: 'example note 2',
      time: '12:05:00',
      language: ['french']
    },
    {
      uuid: 3,
      date: '2020-12-26',
      note: 'example note 3',
      time: '12:05:00',
      language: ['spanish']
    },
    {
      uuid: 4,
      date: '2020-12-26',
      note: 'example note 4',
      time: '12:05:00',
      language: ['chinese']
    },
  ]

to this new array grouped by date到这个按日期分组的新数组

[
    {
    date:'2020-12-26',
    details:[
      {
        uuid: 3,
        date: '2020-12-26',
        note: 'example note 3',
        time: '12:05:00',
        language: ['spanish']
      },
      {
        uuid: 4,
        date: '2020-12-26',
        note: 'example note 4',
        time: '12:05:00',
        language: ['chinese']
      },
    ]
  },
    {
    date:'2020-12-25',
    details:[
      {
        uuid: 1,
        date: '2020-12-26',
        note: 'example note 1',
        time: '12:05:00',
        language: ['english']
      },
      {
        uuid: 2,
        date: '2020-12-26',
        note: 'example note 2',
        time: '12:05:00',
        language: ['french']
      },
    ]
  },
  ]

i have tried it using lodash _.groupBy but that is not what i want my array to look like, i tried doing this combining, .filter, .reduce and.map but it is really lengthy and does not look efficient at all我已经尝试使用 lodash _.groupBy 但这不是我希望我的数组看起来的样子,我尝试将 .filter、.reduce 和 .map 组合起来,但它真的很长而且看起来一点也不高效

I would use .reduce() with .find() as:我会使用 .reduce( .reduce().find()作为:

 const data = [{ uuid: 1,date: '2020-12-25',note: 'example note 1',time: '12:05:00',language: ['english']},{uuid: 2,date: '2020-12-25',note: 'example note 2',time: '12:05:00',language: ['french']},{uuid: 3,date: '2020-12-26',note: 'example note 3',time: '12:05:00',language: ['spanish']}, {uuid: 4,date: '2020-12-26',note: 'example note 4',time: '12:05:00',language: ['chinese']},] const result = data.reduce((a, c) => { const found = a.find(e => e.date === c.date); const elem = { uuid: c.uuid, note: c.note, time: c.time, language: c.language }; if (found) { found.details.push(elem); } else { a.push({ date: c.date, details: [elem] }); } return a; }, []); console.log(result)

You can use the reduce function to create groups and assign the values to them:您可以使用reduce function 创建组并将值分配给它们:

 const input = [ { uuid: 1, date: '2020-12-25', note: 'example note 1', time: '12:05:00', language: ['english'] }, { uuid: 2, date: '2020-12-25', note: 'example note 2', time: '12:05:00', language: ['french'] }, { uuid: 3, date: '2020-12-26', note: 'example note 3', time: '12:05:00', language: ['spanish'] }, { uuid: 4, date: '2020-12-26', note: 'example note 4', time: '12:05:00', language: ['chinese'] }, ]; const output = input.reduce((result, record) => { if (.result[record.date]) result[record:date] = { date. record,date: details; [] }. result[record.date].details;push(record); return result, }; {}). console;log(output). // If you want it as an array console.log(Object;values(output));

Instead of creating it directly as an array, I create it as an object with the date as the key.我没有直接将其创建为数组,而是将其创建为 object,并以日期为键。 This makes it so I don't have to search all of the results for the correct one.这样我就不必在所有结果中搜索正确的结果。

I start by looking if I have an entry for that date.我首先查看我是否有该日期的条目。 If I don't, I make one.如果我没有,我会做一个。

Then I simply add the record to the details for that entry.然后我只需将记录添加到该条目的详细信息中。

At the end, I convert it to an array if you really need it that way simply by using Object.values() .最后,如果您真的需要它,我只需使用Object.values()将其转换为数组。

Ciao, you could do a lodash chain , then a groupBy follewed by a map . Ciao,你可以做一个 lodash chain ,然后是一个groupBy ,后面跟着一个map Like this:像这样:

 let input = [ { uuid: 1, date: '2020-12-25', note: 'example note 1', time: '12:05:00', language: ['english'] }, { uuid: 2, date: '2020-12-25', note: 'example note 2', time: '12:05:00', language: ['french'] }, { uuid: 3, date: '2020-12-26', note: 'example note 3', time: '12:05:00', language: ['spanish'] }, { uuid: 4, date: '2020-12-26', note: 'example note 4', time: '12:05:00', language: ['chinese'] }, ] console.log( _.chain(input) // Group the elements of Array based on `date` property.groupBy("date").map((value, key) => ({ date: key, details: value })).value() );
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>

Here is a solution: https://jsfiddle.net/9p1fx3dq/1/这是一个解决方案: https://jsfiddle.net/9p1fx3dq/1/

    var a = [
        {
          uuid: 1,
          date: '2020-12-25',
          note: 'example note 1',
          time: '12:05:00',
          language: ['english']
        },
        {
          uuid: 2,
          date: '2020-12-25',
          note: 'example note 2',
          time: '12:05:00',
          language: ['french']
        },
        {
          uuid: 3,
          date: '2020-12-26',
          note: 'example note 3',
          time: '12:05:00',
          language: ['spanish']
        },
        {
          uuid: 4,
          date: '2020-12-26',
          note: 'example note 4',
          time: '12:05:00',
          language: ['chinese']
        },
      ];
      
      function groupBy(key) {
      return function group(array) {
        return array.reduce((acc, obj) => {
          const property = obj[key];
          if (!acc.some(s => s.date === property)){
            acc.push({date: property, details: []});
          }
          var details = acc.find(s => s.date === property).details;
          details.push(obj);
          return acc;
        }, []);
      };
    }
    
    console.log(groupBy('date')(a));

I used new Set to get the distinct values我使用new Set来获取不同的值

const array1 = [
  {
    uuid: 1,
    date: "2020-12-25",
    note: "example note 1",
    time: "12:05:00",
    language: ["english"]
  },
  {
    uuid: 2,
    date: "2020-12-25",
    note: "example note 2",
    time: "12:05:00",
    language: ["french"]
  },
  {
    uuid: 3,
    date: "2020-12-26",
    note: "example note 3",
    time: "12:05:00",
    language: ["spanish"]
  },
  {
    uuid: 4,
    date: "2020-12-26",
    note: "example note 4",
    time: "12:05:00",
    language: ["chinese"]
  }
];

const dates = array1.map(x => x.date);
const uniqueDates = [...new Set(dates)];
const result = uniqueDates.map(date => {
  return {
    date: date,
    details: array1.filter(f => f.date === date)
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM