简体   繁体   English

映射对象键:值对到JSON的对象数组的数组

[英]Mapping object key : value pair to array of object arrays for JSON

I'm trying to convert the following array into something JSON can understand and be able to append the same key/value pair to each array of objects, but I'm getting stuck on how to go about it. 我正在尝试将以下数组转换为JSON可以理解的东西,并能够将相同的键/值对附加到每个对象数组,但是我对如何使用它感到困惑。

Given the following array of objects: 给定以下对象数组:

{
  "array": [
    {
      "Type": "Current",
      "Item1": "3",
      "Item2": "23",
      "Item3": "90",
      "Item4": null,
      "Year": "2019",
      "Amount": "100"
    },
    {
      "Type": "Current",
      "Item1": "3",
      "Item2": "23",
      "Item3": "90",
      "Item4": null,
      "Year": "2020",
      "Amount": "200"
    },
    {
      "Type": "Current",
      "Item1": "3",
      "Item2": "23",
      "Item3": "90",
      "Item4": null,
      "Year": "2021",
      "Amount": "300"
    },
    {
      "Type": "Change",
      "Item1": null,
      "Item2": null,
      "Item3": null,
      "Item4": null,
      "Year": "2019"
    },
    {
      "Type": "Change",
      "Item1": null,
      "Item2": null,
      "Item3": null,
      "Item4": null,
      "Year": "2020",
      "Amount": ""
    },
    {
      "Type": "Change",
      "Item1": null,
      "Item2": null,
      "Item3": null,
      "Item4": null,
      "Year": "2021",
      "Amount": ""
    }
  ]
}

I need to add the following to each array: 我需要将以下内容添加到每个数组:

{Title : "title", id : "idNum"}

So that it reads like: 这样它看起来像:

{
  "Title": "title",
  "ID": "idNum",
  "Type": "Current",
  "Item1": "3",
  "Item2": "23",
  "Item3": "90",
  "Item4": null,
  "Year": "2019",
  "Amount": "100"
},
{
  "Title": "title",
  "ID": "idNum",
  "Type": "Current",
  "Item1": "3",
  "Item2": "23",
  "Item3": "90",
  "Item4": null,
  "Year": "2020",
  "Amount": "200"
},

etc. 等等

I'm not even sure if this is actually an array of object arrays, as I'm admittedly guessing a bit on the terminology. 我什至不确定这是否实际上是对象数组的数组,因为我承认对术语有些猜测。 Eventually, I need this to be able to be processed as a JSON object for submitting to a SharePoint list using AJAX. 最终,我需要能够将其作为JSON对象进行处理,以便使用AJAX提交到SharePoint列表。

Would I need to loop through each array within the array and then add the object key/value pairs? 我是否需要遍历数组中的每个数组,然后添加对象键/值对?

You can use Array.map and spread and not mutate the original array: 您可以使用Array.mapspread 而不 Array.map原始数组:

 const data = { "array": [ { "Type": "Current", "Item1": "3", "Item2": "23", "Item3": "90", "Item4": null, "Year": "2019", "Amount": "100" }, { "Type": "Current", "Item1": "3", "Item2": "23", "Item3": "90", "Item4": null, "Year": "2020", "Amount": "200" }, { "Type": "Current", "Item1": "3", "Item2": "23", "Item3": "90", "Item4": null, "Year": "2021", "Amount": "300" }, { "Type": "Change", "Item1": null, "Item2": null, "Item3": null, "Item4": null, "Year": "2019" }, { "Type": "Change", "Item1": null, "Item2": null, "Item3": null, "Item4": null, "Year": "2020", "Amount": "" }, { "Type": "Change", "Item1": null, "Item2": null, "Item3": null, "Item4": null, "Year": "2021", "Amount": "" } ] } result = data.array.map(x => ({ Title: 'Title', ID: 'idNum', ...x})) console.log(result) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM