包装类型为字符串的对象属性 - JavaScript
为此,请结合使用 Object.keys() 和 reduce()。为了显示结果,我们还需要使用 concat()。
示例
以下是代码 -
var details = { name: ["John", "David"], age1: "21", age2: "23" },
output = Object
.keys(details)
.reduce((obj, tempKey) =>
(obj[tempKey] = [].concat(details[tempKey]), obj), {})
console.log(output) 要运行以上程序,你需要使用以下命令 -
node fileName.js.
在此,我的文件名是 demo302.js。
输出
它将在控制台上输出以下内容 -
PS C:\Users\Amit\javascript-code> node demo302.js
{ name: [ 'John', 'David' ], age1: [ '21' ], age2: [ '23' ] }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP