【问题标题】:Restructuring an array into a list of objects in a object Javascript/Nodejs将数组重构为对象Javascript / Nodejs中的对象列表
【发布时间】:2021-05-12 14:40:21
【问题描述】:

我想要的输出在这篇文章的底部,我想删除数组并将对象列表放入对象中。

我正在分享我的地图功能,因为我希望里面有一种方法可以获得我想要的输入,而我只是做得不正确。

如果我所做的没有意义,我会喜欢一种更好的方法来处理这些数据。

起始数据(./updatepayloadTEMP.json)

{
 "outputparameters": [
    {
        "name": "0000x0000",
        "filepath": "D:\\Code\\ImageTiling\\6\\0000x0000.png"
    },
    {
        "name": "0000x0001",
        "filepath": "D:\\Code\\ImageTiling\\6\\0000x0001.png"
    },
    {
        "name": "0000x0002",
        "filepath": "D:\\Code\\ImageTiling\\6\\0000x0002.png"
    }
 ]
}

变量

 let UpdatedTaskOutput = fs.readFileSync('./updatepayloadTEMP.json'); 
 let Updatedtaskoutputjson = JSON.parse(UpdatedTaskOutput); 
 var dynamictaskdetails = Updatedtaskoutputjson.outputparameters;

地图功能

   var taskparamscompiled = dynamictaskdetails.map(function (elem) {
   taskname = tasknamefromworkflowdef + elem.name;
   taskparms = taskparamsobj;
   return {
    [taskname]: taskparms,
   };
  });

我目前得到的taskparamscompiled

 [
 {
   process0000x0000: {
    tr: 16,
    tc: 16,
    ofr: 16,
    ofc: 16,
    outfile: '"D:\\Code\\Process\\1"',
   },
  },
{
 process0000x0001: {
   tr: 16,
   tc: 16,
   ofr: 16,
   ofc: 16,
   outfile: '"D:\\Code\\Process\\1"',
  },
},
{
 process0000x0002: {
   tr: 16,
   tc: 16,
   ofr: 16,
   ofc: 16,
   outfile: '"D:\\Code\\Process\\1"',
 },
},
];

我想要什么

 {
   "process0000x0000": {
   "tr": 16,
   "tc": 16,
   "ofr": 16,
   "ofc": 16,
   "outfile": '"D:\\Code\\Process\\1"'
 },

    "process0000x0001": {
   "tr": 16,
   "tc": 16,
   "ofr": 16,
   "ofc": 16,
   "outfile": '"D:\\Code\\Process\\1"'
 },
 "process0000x0002": {
   "tr": 16,
   "tc": 16,
   "ofr": 16,
   "ofc": 16,
   "outfile": '"D:\\Code\\Process\\1"'
 },
}

我错过了一些东西,更新: 我现在需要像这样将文件路径( "filepath": "D:\\Code\\ImageTiling\\6\\0000x0000.png") 放入进程对象中

process0000x0000: {
   filepath: "D:\\Code\\ImageTiling\\6\\0000x0000.png" 
    tr: 16,
    tc: 16,
    ofr: 16,
    ofc: 16,
    outfile: '"D:\\Code\\Process\\1"',
   }

【问题讨论】:

    标签: javascript node.js arrays object


    【解决方案1】:

    Array.prototype.map() 将返回一个数组。您可能想使用Array.prototype.reduce()https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

    我认为这会给你想要的输出

    const dynamictaskdetails = [
      {
        name: '0000x0000',
        filepath: 'D:\\Code\\ImageTiling\\6\\0000x0000.png'
      },
      {
        name: '0000x0001',
        filepath: 'D:\\Code\\ImageTiling\\6\\0000x0001.png'
      },
      {
        name: '0000x0002',
        filepath: 'D:\\Code\\ImageTiling\\6\\0000x0002.png'
      }
    ];
    
    const taskparamsobj = {
      tr: 16,
      tc: 16,
      ofr: 16,
      ofc: 16,
      outfile: 'D:\\Code\\Process\\1'
    };
    
    const taskparamscompiled = dynamictaskdetails.reduce((accumulator, elem) => {
      const taskname = 'process' + elem.name;
      return {
        ...accumulator,
        [taskname]: taskparamsobj,
      };
    }, {});
    
    console.log(taskparamscompiled);
    

    这导致以下输出:

    {
      process0000x0000: { tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' },
      process0000x0001: { tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' },
      process0000x0002: { tr: 16, tc: 16, ofr: 16, ofc: 16, outfile: 'D:\\Code\\Process\\1' }
    }
    

    【讨论】:

    • 非常感谢,我感觉我没有正确使用 Array.prototype.map()
    • idk 如果我应该在这里回复或询问,但是,如果我想将 ./updatepayloadTEMP.json 中的文件路径添加到具有其他属性的子对象中怎么办?
    • 最后的努力,我以我目前的状态发表了另一篇文章。 stackoverflow.com/questions/66230508/…@josh-pospisil
    猜你喜欢
    • 1970-01-01
    • 2021-09-20
    • 2017-06-11
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 2021-10-01
    相关资源
    最近更新 更多