admin管理员组文章数量:1026989
I have an array:
[
{"allocateProd1": "30.0000"},
{"allocateProd2": "0"},
{"allocateProd3": "0"},
{"allocateProd4": "30"}
]
This array is dynamically generated as in the number of objects inside varies depending on data
I need an object:
{
"allocateProd1": "30.0000",
"allocateProd2": "0",
"allocateProd3": "0",
"allocateProd4": "30"
}
Mostly going to use it in React. JS/ES6 solution will help
I have an array:
[
{"allocateProd1": "30.0000"},
{"allocateProd2": "0"},
{"allocateProd3": "0"},
{"allocateProd4": "30"}
]
This array is dynamically generated as in the number of objects inside varies depending on data
I need an object:
{
"allocateProd1": "30.0000",
"allocateProd2": "0",
"allocateProd3": "0",
"allocateProd4": "30"
}
Mostly going to use it in React. JS/ES6 solution will help
Share Improve this question edited Feb 24, 2019 at 2:27 Ele 33.8k7 gold badges43 silver badges80 bronze badges asked Feb 24, 2019 at 2:26 Rajdeep ChowdhuriRajdeep Chowdhuri 652 silver badges8 bronze badges 1- "This array is dynamically generated as in the number of objects inside varies depending on data" Is the expected result for property names to be overwritten? – guest271314 Commented Feb 24, 2019 at 3:05
2 Answers
Reset to default 6An alternative is using the function reduce
+ spread syntax
.
let arr = [
{"allocateProd1": "30.0000"},
{"allocateProd2": "0"},
{"allocateProd3": "0"},
{"allocateProd4": "30"}
];
let result = arr.reduce((a, c) => ({...a, ...c}), Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
The alternative is using the function Object.assign
let arr = [
{"allocateProd1": "30.0000"},
{"allocateProd2": "0"},
{"allocateProd3": "0"},
{"allocateProd4": "30"}
];
let result = arr.reduce((a, c) => Object.assign(a, c), Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Just for pleteness by taking the array and spread ...
the objects into Object.assign
with an object as target. Voilà!
var array = [{ allocateProd1: "30.0000"}, { allocateProd2: "0"}, { allocateProd3: "0"}, { allocateProd4: "30"}],
object = Object.assign({}, ...array);
console.log(object);
I have an array:
[
{"allocateProd1": "30.0000"},
{"allocateProd2": "0"},
{"allocateProd3": "0"},
{"allocateProd4": "30"}
]
This array is dynamically generated as in the number of objects inside varies depending on data
I need an object:
{
"allocateProd1": "30.0000",
"allocateProd2": "0",
"allocateProd3": "0",
"allocateProd4": "30"
}
Mostly going to use it in React. JS/ES6 solution will help
I have an array:
[
{"allocateProd1": "30.0000"},
{"allocateProd2": "0"},
{"allocateProd3": "0"},
{"allocateProd4": "30"}
]
This array is dynamically generated as in the number of objects inside varies depending on data
I need an object:
{
"allocateProd1": "30.0000",
"allocateProd2": "0",
"allocateProd3": "0",
"allocateProd4": "30"
}
Mostly going to use it in React. JS/ES6 solution will help
Share Improve this question edited Feb 24, 2019 at 2:27 Ele 33.8k7 gold badges43 silver badges80 bronze badges asked Feb 24, 2019 at 2:26 Rajdeep ChowdhuriRajdeep Chowdhuri 652 silver badges8 bronze badges 1- "This array is dynamically generated as in the number of objects inside varies depending on data" Is the expected result for property names to be overwritten? – guest271314 Commented Feb 24, 2019 at 3:05
2 Answers
Reset to default 6An alternative is using the function reduce
+ spread syntax
.
let arr = [
{"allocateProd1": "30.0000"},
{"allocateProd2": "0"},
{"allocateProd3": "0"},
{"allocateProd4": "30"}
];
let result = arr.reduce((a, c) => ({...a, ...c}), Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
The alternative is using the function Object.assign
let arr = [
{"allocateProd1": "30.0000"},
{"allocateProd2": "0"},
{"allocateProd3": "0"},
{"allocateProd4": "30"}
];
let result = arr.reduce((a, c) => Object.assign(a, c), Object.create(null));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Just for pleteness by taking the array and spread ...
the objects into Object.assign
with an object as target. Voilà!
var array = [{ allocateProd1: "30.0000"}, { allocateProd2: "0"}, { allocateProd3: "0"}, { allocateProd4: "30"}],
object = Object.assign({}, ...array);
console.log(object);
本文标签: javascriptCreate a single object from an array of objectsJSES6Stack Overflow
版权声明:本文标题:javascript - Create a single object from an array of objects - JSES6 - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745656699a2161637.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论