将对象数组转换为字符串es6

时间:2018-06-19 06:07:48

标签: javascript ecmascript-6

如何转换以下对象数组

let user =[
{key: "name", value: "xyz"},
{key: "friend", value: "abc"},
{key: "mobileno", value: "123"}];

进入字符串输出,如

"name:xyz
friend:abc
mobileno:123"

到目前为止我已经完成了这个

let person = [
{key: "name ",value: "harsh" },
{key: "friend ",value: "dhruvil" }, 
{key: "mobileno ",value: "982559061" }];

var str = JSON.stringify(person); 
var newstr = str.replace(/[^a-xyz]/g, ''); 
console.log(newstr);

1 个答案:

答案 0 :(得分:0)

您可以先使用$svn = 'C:\Program Files\TortoiseSVN\bin\svn.exe' $commitmsg = 'C:\TradeSupport\AB Reports\msg.txt' $Reports = Get-Content -Path 'C:\TradeSupport\AB Reports\AB Reports.csv' Foreach ($Report in $Reports){ & $svn remove --force C:\SVN\Test\$Report & $svn commit -F $commitmsg C:\SVN\Test\$Report} & $svn update $commitmsg 'C:\SVN\Test' 或您喜欢的方式将数组转换为所需的对象,例如

.reduce

现在您可以使用user.reduce((o, key) => Object.assign(o, {[key.key]: key.value}), {}) 将此对象转换为字符串,然后在此字符串上使用JSON.stringify方法将花括号.replace替换为

{



replace(/[\{\}]/g, "")