将不同的数组变量值合并到 JSON 对象中

Merge different Array variables value into JSON Object

我在下面的各个变量中保存了一个数组中的账单#。

billA = [12345]  // For "Pizzahut" Billsystem 

billB = [96754,75432]  // For "Subway" Billsystem

billC = [63946]   // For "XYZ" Billsystem

我打算将上述值组合成一个 JSON 对象,如下面的格式。

结果输出:

{"BillDetails" : [{billsystem: "PizzaHut" , billnum: "12345"}, {billsystem : "Subway" , billnum "96754" }, {billsystem : "Subway" , billnum "75432"},{billsystem : "XYZ" , billnum "63946"}]}
const obj = {
   "BillDetails" : [
       ...billA.map(item=>({billsystem: "PizzaHut" , billnum: String(item)})),
       ...billB.map(item=>({billsystem: "Subway" , billnum: String(item)})),
       ...billC.map(item=>({billsystem: "XYZ" , billnum: String(item)})),
   ]
}

您可以使用 flatMap 以及一系列计费系统

let billA = [12345] // For "Pizzahut" Billsystem 
let billB = [96754, 75432] // For "Subway" Billsystem
let billC = [63946] // For "XYZ" Billsystem
let billD = [] // For "Testing" Billsystem
let types = ["Pizzahut", "Subway", "XYZ", "Testing"]
let json = {
  BillDetails: [billA, billB, billC, billD].flatMap((e, i) => (e?.map(b => ({
    billSystem: types[i],
    billNum: b
  }))))
}

console.log(json)