将数组重组为对象Javascript/Nodejs中的对象列表 [英] Restructuring an array into a list of objects in a object Javascript/Nodejs
问题描述
我想要的输出在这篇文章的底部,我想删除数组并将对象列表放到对象内部.
My desired output is at the bottom of this post, I want to remove the array and put a list of objects inside an object.
我正在共享自己的地图函数,因为我希望其中有一个方法可以获取所需的输入,而我只是做得不正确.
I'm sharing my map function because i'm hopeful that there's a method inside there that can get my desired input and i'm just not doing it correctly.
如果我在做的事情没有意义,我会喜欢一种更好的方法来处理这些数据.
If what i'm doing doesn't make sense i would love a better method of manipulating this data.
启动数据(./updatepayloadTEMP.json)
Starting Data(./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,
};
});
已编译的taskparams
[
{
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")
放入这样的过程对象中
I missed something, UPDATE:
I now need to get the filepath ( "filepath": "D:\\Code\\ImageTiling\\6\\0000x0000.png")
into the process object like this
process0000x0000: {
filepath: "D:\\Code\\ImageTiling\\6\\0000x0000.png"
tr: 16,
tc: 16,
ofr: 16,
ofc: 16,
outfile: '"D:\\Code\\Process\\1"',
}
推荐答案
Array.prototype.map()
将返回一个数组.您可能要使用 Array.prototype.reduce()
. https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
Array.prototype.map()
will return an array. You probably want to use Array.prototype.reduce()
. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
我认为这会为您提供所需的输出
I think this gives you your desired output
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' }
}
这篇关于将数组重组为对象Javascript/Nodejs中的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!