Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

js合并JSON时,replace替换中括号的问题

以下两个JSON需要合并

[
  {
    "playerName": "小张",
    "updatedAt": "2017-03-28 10:03:57"
  }
]
[
  {
    "playerName": "小王",
    "updatedAt": "2017-03-28 10:03:57",
  }
]

需要把下面的JSON拼接成一个:

[{"createdAt":"2017-03-25 13:38:55"playerName":"测试帐号"}][{"createdAt":"2017-03-28 10:03:57"playerName":"小王"}]

希望的结果为:

[{"createdAt":"2017-03-25 13:38:55"playerName":"测试帐号"},{"createdAt":"2017-03-28 10:03:57"playerName":"小王"}]

我用的代码为:

dataC = eval('('+(JSON.stringify(dataI.results)+JSON.stringify(dataB.results)).replace('[]',',')+')');

请问如何能将把JSON中段的 [] 替换成 ,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
dataC = [].concat(dataI.results).concat(dataB.results)

瞎捣鼓啥?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...