如何在 JavaScript 中分离每个 JSON 对象并将它们分组到一个数组中?

来自 multer.js 的 req.files 示例


[

    {

        fieldname: files0,

        originalname: 'Screenshot from 2020-05-23 01-52-30.png',

        encoding: '7bit',

        mimetype: 'image/png',

        destination: 'public/uploads/projects/0a480789-eb14-4fe4-bb89-44aa741efe33/proposals/chma00000133',

        filename: '1606822252923_Screenshot from 2020-05-23 01-52-30.png',

        path: 'public/uploads/projects/0a480789-eb14-4fe4-bb89-44aa741efe33/proposals/chma00000133/1606822252923_Screenshot from 2020-05-23 01-52-30.png',

        size: 172092

      },

      {

        fieldname: files0,

        originalname: 'Screenshot from 2020-05-22 08-44-05.png',

        encoding: '7bit',

        mimetype: 'image/png',

        destination: 'public/uploads/projects/0a480789-eb14-4fe4-bb89-44aa741efe33/proposals/chma00000133',

        filename: '1606822252925_Screenshot from 2020-05-22 08-44-05.png',

        path: 'public/uploads/projects/0a480789-eb14-4fe4-bb89-44aa741efe33/proposals/chma00000133/1606822252925_Screenshot from 2020-05-22 08-44-05.png',

        size: 440592

      },

      {

        fieldname: files1,

        originalname: 'Screenshot from 2020-05-30 11-36-42.png',

        encoding: '7bit',

        mimetype: 'image/png',

        destination: 'public/uploads/projects/0a480789-eb14-4fe4-bb89-44aa741efe33/proposals/chma00000133',

        filename: '1606822252930_Screenshot from 2020-05-30 11-36-42.png',

        path: 'public/uploads/projects/0a480789-eb14-4fe4-bb89-44aa741efe33/proposals/chma00000133/1606822252930_Screenshot from 2020-05-30 11-36-42.png',

        size: 477511

      },


我想动态地将每个 JSON 对象与其字段名分组,并将它们存储在对象数组中,同时使用它们的字段名编号作为数组索引。


组可以在 0 到 9 之间,具体取决于用户输入。


开满天机
浏览 127回答 3
3回答

潇潇雨雨

我会使用减少const data = [ { fieldname: 'files0', originalname: 'Screenshot from 2020-05-23 01-52-30.png', },{ fieldname: 'files0', originalname: 'Screenshot from 2020-05-22 08-44-05.png', },{ fieldname: 'files1', originalname: 'Screenshot from 2020-05-30 11-36-42.png', },{ fieldname: 'files1', originalname: 'Screenshot from 2020-05-30 11-36-36.png', },{ fieldname: 'files1', originalname: 'Screenshot from 2020-05-30 11-36-31.png', }],arr = data.reduce((acc, cur) => {  const idx = cur.fieldname.replace("files",""); // get 0, 1  if (acc[idx]) acc[idx].push(cur); // if already there, just push  else acc[idx] = [cur];            // otherwise initialise  return acc;},[]) console.log(arr)注意:组可以在 0 到 9 之间,具体取决于用户输入。我假设没有漏洞,因为这可能会导致稀疏数组,所以data = [      { fieldname : 'files0' },     { fieldname : 'files0' },     { fieldname : 'files3' },     { fieldname : 'files3' }, }会创建一个数组[  [ { fieldname : 'files0' }, { fieldname : 'files0' } ],  null,  null,  [ { fieldname : 'files3' }, { fieldname : 'files3' } ],]

肥皂起泡泡

我尽量不修改你的逻辑来做到这一点。如果没有files2,filesArray[2] 将为undefined。// Omit other fieldsconst req = {    files: [        { fieldname: "files0", originalname: 'Screenshot from 2020-05-23 01-52-30.png'},        { fieldname: "files0", originalname: 'Screenshot from 2020-05-22 08-44-05.png'},        { fieldname: "files1", originalname: 'Screenshot from 2020-05-30 11-36-42.png'},        { fieldname: "files1", originalname: 'Screenshot from 2020-05-30 11-36-36.png'},        { fieldname: "files1", originalname: 'Screenshot from 2020-05-30 11-36-31.png'},        { fieldname: "files3", originalname: 'Screenshot from 2020-05-30 11-36-42.png'},        { fieldname: "files3", originalname: 'Screenshot from 2020-05-30 11-36-36.png'},        { fieldname: "files3", originalname: 'Screenshot from 2020-05-30 11-36-31.png'}    ]}let filesArray = []for (let file of req.files) {    const thisFileGroup = parseInt(file.fieldname.match(/\d/g).join(''), 10);    if (!filesArray[thisFileGroup]) {        filesArray[thisFileGroup] = []    }    filesArray[thisFileGroup].push(file)}console.log(filesArray[2]) // will be undefinedconsole.log(filesArray[1]) // will be files1 arrayconsole.log(filesArray)

DIEA

如果您不一定需要按索引排序files[index]在这种情况下,你可以将Array.prototype.reduce()转换为Object。您的对象将按属性(但不是按字母顺序)分组,其中包含一个数组,其中的项目具有相同的属性键。使用Object.values() (如果需要,还可以使用Array.prototype.forEach()) 迭代您的对象。fieldnameconst data = [  { fieldname: "files0", originalname: '0-a'},  { fieldname: "files0", originalname: '0-b'},  { fieldname: "files1", originalname: '1-a'},  { fieldname: "files1", originalname: '1-b'},  { fieldname: "files1", originalname: '1-c'},];const byFieldName = data.reduce((ob, d) => {  if (!ob.hasOwnProperty(d.fieldname)) ob[d.fieldname] = [];  ob[d.fieldname].push(d);  return ob;}, {});// console.log(byFieldName); // Uncomment to see how it looks likeconsole.log( Object.values(byFieldName) );
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript