Skip to content

Latest commit

 

History

History
33 lines (20 loc) · 993 Bytes

flags.md

File metadata and controls

33 lines (20 loc) · 993 Bytes
标题 标签
findLastN(字符串数组转换为对象) array,object(数组,对象)

将字符串数组转换为对象映射为真。

  • 使用 Array.prototype.reduce() 将数组转换为对象,其中每个数组值都用作值设置为 true 的键。

代码如下:

const flags = arr => arr.reduce((acc, item) => ({ ...acc, [item]: true }), {});

ts 代码如下:

调用方式:

flags(['red', 'green']); // { red: true, green: true }

应用场景

以下是一个实战示例:

结果如下:

<iframe src="codes/javascript/html/flags.html"></iframe>