在GAS中合并2个JSON对象(无jquery)

时间:2016-04-22 13:26:47

标签: json google-sheets javascript-objects

如何合并2个(结构相同的)JSON对象(在Google电子表格脚本中)?

{"records":[{"id":28100988,"work_text_reviews_count":13,"average_rating":"3.10"},{"id":10280687,"work_text_reviews_count":80,"average_rating":"3.87"}]}

{"records":[{"id":16135639,"work_text_reviews_count":0,"average_rating":"0.00"},{"id":17978337,"work_text_reviews_count":2414,"average_rating":"3.76"{"id":360721218,"work_text_reviews_count":4924,"average_rating":"3.98"}]}

合并后,结果应为:

{"records":[{"id":28100988,"work_text_reviews_count":13,"average_rating":"3.10"},{"id":10280687,"work_text_reviews_count":80,"average_rating":"3.87"},{"id":16135639,"work_text_reviews_count":0,"average_rating":"0.00"},{"id":360721218,"work_text_reviews_count":4924,"average_rating":"3.98"}]}

因为在Google Spreadsheets中我不能使用JQuery,而且我花了一整天时间尝试使用javascript解决方案,这些解决方案无法正常运行。

1 个答案:

答案 0 :(得分:1)

您可以使用concat

var json1 = {
  "records": [{
    "id": 28100988,
    "work_text_reviews_count": 13,
    "average_rating": "3.10"
  }, {
    "id": 10280687,
    "work_text_reviews_count": 80,
    "average_rating": "3.87"
  }]
}

var json2 = {
  "records": [{
    "id": 16135639,
    "work_text_reviews_count": 0,
    "average_rating": "0.00"
  }, {
    "id": 17978337,
    "work_text_reviews_count": 2414,
    "average_rating": "3.76"
  }, {
    "id": 360721218,
    "work_text_reviews_count": 4924,
    "average_rating": "3.98"
  }]
}


var json3 = json1.records.concat(json2.records);