我们如何使用 reduce 和 rest 运算符将以下数组转换为对象
Posted
技术标签:
【中文标题】我们如何使用 reduce 和 rest 运算符将以下数组转换为对象【英文标题】:How can we convert below arrays into objects using reduce and rest operator 【发布时间】:2021-12-24 02:55:02 【问题描述】:谁能帮我将以下数组转换为对象:
let data = [
name: "A", job: "soft", address: "ggn" ,
name: "B", job: "software", address: "GGN" ,
name: "C", job: "software", address: "GGN" ,
];
Output:
“A”:job: “software”, address: “GGN”,
“B”:job: “software”, address: “GGN”,
“C”:job: “software”, address: “GGN”
;
这里我不想要对象的名称及其值。
我尝试使用以下解决方案:
const output = data.reduce((object, x) =>
return ...object, [x.name]: x ;
, );
但我得到了不同的答案。 有人可以通过提供最佳解决方案来提供帮助。我们可以使用 reduce 和 rest 运算符来解决它。
【问题讨论】:
【参考方案1】:你可以这样使用它:
let data = [
name: "A", job: "soft", address: "ggn" ,
name: "B", job: "software", address: "GGN" ,
name: "C", job: "software", address: "GGN" ,
];
const output = data.reduce((acc, curr) =>
const name, ...rest = curr;
acc[name] = rest
return acc;
, );
或者:
const output = data.reduce((acc, curr) =>
const name, ...rest = curr;
return ...acc, [name]: rest ;
, );
【讨论】:
【参考方案2】:最简单的方法是首先对对象进行解构,这样您就可以将名称分开。然后你可以把它们和累加器一起返回。
const data = [
name: "A", job: "software", address: "GGN" ,
name: "B", job: "software", address: "GGN" ,
name: "C", job: "software", address: "GGN" ,
];
const output = data.reduce((acc, item) =>
const name, ...rest = item;
return
...acc,
[name]: rest,
, )
console.log(output)
【讨论】:
【参考方案3】:这里不需要reduce
。
Object.fromEntries()
救援 - 您可以使用 .map
和解构将对象映射到对数组中,fromEntries
可以变成普通对象:
> let data = [
name: "A", job: "soft", address: "ggn" ,
name: "B", job: "software", address: "GGN" ,
name: "C", job: "software", address: "GGN" ,
];
> Object.fromEntries(data.map((name, ...rest) => [name, rest]));
A: job: 'soft', address: 'ggn'
B: job: 'software', address: 'GGN',
C: job: 'software', address: 'GGN'
【讨论】:
【参考方案4】:您可以将.map()
和.reduce()
与Object.assign()
结合使用,如下所示:
let output = data.map((name,...rest) => ([name]:rest))
.reduce((acc,cur) => Object.assign(acc,cur),);
演示
let data = [
name: "A", job: "soft", address: "ggn",
name: "B", job: "software", address: "GGN",
name: "C", job: "software", address: "GGN",
];
let output = data.map((name,...rest) => ([name]:rest))
.reduce((acc,cur) => Object.assign(acc,cur),);
console.log( output );
或者,您可以使用.map()
和.reduce()
,如下所示:
let output = data.map((name,...rest) => ([name]:rest))
.reduce((acc,cur) => (...acc,...cur),);
演示
let data = [
name: "A", job: "soft", address: "ggn",
name: "B", job: "software", address: "GGN",
name: "C", job: "software", address: "GGN",
];
let output = data.map((name,...rest) => ([name]:rest))
.reduce((acc,cur) => (...acc,...cur),);
console.log( output );
或者,如果您的对象没有太多属性:
let output = data.reduce((acc,cur) => (...acc,...[cur.name]: job:cur.job,address:cur.address),);
演示
let data = [
name: "A", job: "soft", address: "ggn",
name: "B", job: "software", address: "GGN",
name: "C", job: "software", address: "GGN",
];
let output = data.reduce((acc,cur) => (...acc,...[cur.name]: job:cur.job,address:cur.address),);
console.log( output );
【讨论】:
【参考方案5】:let data = [
name: 'A', job: 'soft', address: 'ggn',
name: 'B', job: 'software', address: 'GGN',
name: 'C', job: 'software', address: 'GGN',
];
const output = ;
const obj = data.reduce((acc, el) =>
const name, job, address = el;
return ...acc, [name]: job, address ;
, output);
console.log(obj);
【讨论】:
【参考方案6】:首先你需要改变双引号,它们不是标准的双引号 你可以用 map 方法用更短的代码编写它
let data = [
name: "A", job: "soft", address: "ggn",
name: "B", job: "software", address: "GGN",
name: "C", job: "software", address: "GGN",
];
const toto = data.map ( element =>
const name = element.name
delete element.name
return [name] : element)
console.log(toto)
查看地图方法以了解更多信息:https://learnjsx.com/category/2/posts/es6-mapFunction
【讨论】:
【参考方案7】:你可以这样做
let data = [
name: "A",
job: "soft",
address: "ggn"
,
name: "B",
job: "software",
address: "GGN"
,
name: "C",
job: "software",
address: "GGN"
,
];
const output = data.reduce((object, x) =>
const
name,
job,
address
= x;
object[name] =
job,
address
return object;
, );
console.log(output)
【讨论】:
以上是关于我们如何使用 reduce 和 rest 运算符将以下数组转换为对象的主要内容,如果未能解决你的问题,请参考以下文章