为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

开满天机 2023-09-21 16:45:49
来自 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 之间,具体取决于用户输入。
查看完整描述

3 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

我会使用减少

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' } ],

]


查看完整回答
反对 回复 2023-09-21
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

我尽量不修改你的逻辑来做到这一点。

如果没有files2,filesArray[2] 将为undefined。


// Omit other fields

const 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 undefined

console.log(filesArray[1]) // will be files1 array

console.log(filesArray)


查看完整回答
反对 回复 2023-09-21
?
DIEA

TA贡献1820条经验 获得超2个赞

如果您不一定需要按索引排序files[index]

在这种情况下,你可以将Array.prototype.reduce()转换为Object
您的对象将按属性(但不是按字母顺序)分组,其中包含一个数组,其中的项目具有相同的属性键。使用Object.values() (如果需要,还可以使用Array.prototype.forEach()) 迭代您的对象。fieldname

const 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 like

console.log( Object.values(byFieldName) );


查看完整回答
反对 回复 2023-09-21
  • 3 回答
  • 0 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信