将对象数组转换为数组

编程入门 行业动态 更新时间:2024-10-24 22:17:35
本文介绍了将对象数组转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道这一点已经被问过了,但是我发现的每个解决方案都没有按照我的需要来做。

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);

更多推荐

将对象数组转换为数组

本文发布于:2023-11-30 12:08:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649891.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!