本文介绍了将对象数组转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这一点已经被问过了,但是我发现的每个解决方案都没有按照我的需要来做。
I know this has been asked before, but every solution I found isn't doing it the way I need it.
数组
[ {key: "Title", value: "Default Text"}, {key: "Title2", value: "Default Text2"} ]我需要的对象
{ "Title": "Default Text", "Title2": "Default Text2" }我尝试的一切似乎都返回错误:
Everything I try seems to return this which is wrong:
{ {key: "Title", value: "Default Text"}, {key: "Title2", value: "Default Text2"} }我尝试过在这里找到的大部分东西,我试过的最后一个格式化错误是:
I've tried most things I found here on SO.. the last one I've tried that is formatting it wrong is:
let obj = Object.assign({}, arrayHere);我想要的:
我不断得到但错误:
推荐答案
使用 Array.prototype.reduce() 功能:
var arr = [ {key: "Title", value: "Default Text"}, {key: "Title2", value: "Default Text2"} ], result = arr.reduce((r,o) => {r[o.key] = o.value; return r; }, {}); console.log(result);
更多推荐
将对象数组转换为数组
发布评论