创建一个数组迭代对象数组中的所有对象

时间:2021-05-11 14:00:54

标签: javascript html arrays json object

我有这个 JSON 结构,我想从这个 JSON 数组中创建 2 个数组。一个数组包含从键“仅限员工”到“年度 OOP 最大值/整个家庭”(来自 JSON 数组中的每个对象)的所有值,第二个数组包含从键“初级保健医生访问”到对象末尾的值” .

array 1 = [86.1,30.5,33.2, 291.3, 154.1, 121.2................13700, "$13,100 family"](例如“仅限员工”键来自对象 1 然后是对象 2 然后直到最后一个对象,再次来自对象 1 的下一个键......直到最后一个对象)

同样在数组 2 中,从关键的“初级保健医生访问”开始直到对象结束

array 2 = [“自付额后覆盖 75%”,“35 美元共付额”,35,……,“请参阅 HMO 提供商”] >

我如何在 javascript 中实现这一点?

[
  {
    "Provider ID": 0,
    "Broker ID": 16,
    "[Plan name, employee-facing]": "Contribution Plan",
    "employee Only": 86.1,
    "employee + Spouse/Partner": 291.3,
    "employee + Child(ren)": 121.4,
    "employee + Family": 311.9,
    " max contrib. / employee only": "Credited to your HRA:\n$250",
    " max contrib. / employee + dependents": "Credited to your HRA:\n$500",
    "Annual ded. / employee only": 1750,
    "Annual ded. / employee + dependents": 3500,
    "Annual OOP max / per person": 6850,
    "Annual OOP max / entire famliy": 13700,
    "Primary care doctor visit": "75% covered after deductible",
    "Specialist doctor visit": "75% covered after deductible",
    "Virtual doctor visit": "$4 copay Doctor on Demand",
    "Eligible preventive care": "100% covered, no deductible",
    "Centers of Excellence": "100% covered, no deductible",
    "Urgent care": "75% covered after deductible",
    "Emergency": "$300 copay, then 75% covered after deductible",
    "Hospitalization": "75% covered after deductible",
    "Generic drugs": 4,
    "Brand-name drugs": "$50 or 25% of allowed cost*",
    "Speciality drugs": "$50 or 20% of allowed cost*"
  },
  {
    "Provider ID": 0,
    "Broker ID": 23,
    "[Plan name, employee-facing]": "Premier Plan",
    "employee Only": 30.5,
    "employee + Spouse/Partner": 154.1,
    "employee + Child(ren)": 48.8,
    "employee + Family": 180.8,
    " max contrib. / employee only": "None",
    " max contrib. / employee + dependents": "None",
    "Annual ded. / employee only": 2750,
    "Annual ded. / employee + dependents": 5500,
    "Annual OOP max / per person": 6850,
    "Annual OOP max / entire famliy": 13700,
    "Primary care doctor visit": "$35 copay",
    "Specialist doctor visit": "$75 copay",
    "Virtual doctor visit": "$4 copay Doctor on Demand",
    "Eligible preventive care": "100% covered, no deductible",
    "Centers of Excellence": "100% covered, no deductible",
    "Urgent care": "$75 copay",
    "Emergency": "$300 copay, then 75% covered after deductible",
    "Hospitalization": "75% covered after deductible",
    "Generic drugs": 4,
    "Brand-name drugs": "$50 or 25% of allowed cost*",
    "Speciality drugs": "$50 or 20% of allowed cost*"
  },
  {
    "Benefit/feature": null,
    "Provider ID": 197,
    "[Plan name, employee-facing]": "Kaiser California Low Option South HMO",
    "employee Only": 33.2,
    "employee + Spouse/Partner": 121.2,
    "employee + Child(ren)": 46.7,
    "employee + Family": 138,
    " max contrib. / employee only": "None",
    " max contrib. / employee + dependents": "None",
    "Annual ded. / employee only": "$1,500 individual",
    "Annual ded. / employee + dependents": "$3,000 family",
    "Annual OOP max / per person": "$6,550 individual",
    "Annual OOP max / entire famliy": "$13,100 family",
    "Eligible preventive care services": "100% covered, no deductible",
    "Primary care physician visits": 35,
    "Specialists": 50,
    "Centers of Excellence": "Not available",
    "Virtual doctor visits": "Not available",
    "Hospitalization": "75% covered after deductible",
    "Emergency": "75% covered after deductible",
    "Urgent care": "75% covered after deductible",
    "Generic drugs": 10,
    "Brand-name drugs": 50,
    "Speciality drugs": "See HMO provider"
  }
]

1 个答案:

答案 0 :(得分:1)

const input = [{
    "Provider ID": 0,
    "Broker ID": 16,
    "[Plan name, employee-facing]": "Contribution Plan",
    "employee Only": 86.1,
    "employee + Spouse/Partner": 291.3,
    "employee + Child(ren)": 121.4,
    "employee + Family": 311.9,
    " max contrib. / employee only": "Credited to your HRA:\n$250",
    " max contrib. / employee + dependents": "Credited to your HRA:\n$500",
    "Annual ded. / employee only": 1750,
    "Annual ded. / employee + dependents": 3500,
    "Annual OOP max / per person": 6850,
    "Annual OOP max / entire famliy": 13700,
    "Primary care doctor visit": "75% covered after deductible",
    "Specialist doctor visit": "75% covered after deductible",
    "Virtual doctor visit": "$4 copay Doctor on Demand",
    "Eligible preventive care": "100% covered, no deductible",
    "Centers of Excellence": "100% covered, no deductible",
    "Urgent care": "75% covered after deductible",
    "Emergency": "$300 copay, then 75% covered after deductible",
    "Hospitalization": "75% covered after deductible",
    "Generic drugs": 4,
    "Brand-name drugs": "$50 or 25% of allowed cost*",
    "Speciality drugs": "$50 or 20% of allowed cost*"
  },
  {
    "Provider ID": 0,
    "Broker ID": 23,
    "[Plan name, employee-facing]": "Premier Plan",
    "employee Only": 30.5,
    "employee + Spouse/Partner": 154.1,
    "employee + Child(ren)": 48.8,
    "employee + Family": 180.8,
    " max contrib. / employee only": "None",
    " max contrib. / employee + dependents": "None",
    "Annual ded. / employee only": 2750,
    "Annual ded. / employee + dependents": 5500,
    "Annual OOP max / per person": 6850,
    "Annual OOP max / entire famliy": 13700,
    "Primary care doctor visit": "$35 copay",
    "Specialist doctor visit": "$75 copay",
    "Virtual doctor visit": "$4 copay Doctor on Demand",
    "Eligible preventive care": "100% covered, no deductible",
    "Centers of Excellence": "100% covered, no deductible",
    "Urgent care": "$75 copay",
    "Emergency": "$300 copay, then 75% covered after deductible",
    "Hospitalization": "75% covered after deductible",
    "Generic drugs": 4,
    "Brand-name drugs": "$50 or 25% of allowed cost*",
    "Speciality drugs": "$50 or 20% of allowed cost*"
  },
  {
    "Benefit/feature": null,
    "Provider ID": 197,
    "[Plan name, employee-facing]": "Kaiser California Low Option South HMO",
    "employee Only": 33.2,
    "employee + Spouse/Partner": 121.2,
    "employee + Child(ren)": 46.7,
    "employee + Family": 138,
    " max contrib. / employee only": "None",
    " max contrib. / employee + dependents": "None",
    "Annual ded. / employee only": "$1,500 individual",
    "Annual ded. / employee + dependents": "$3,000 family",
    "Annual OOP max / per person": "$6,550 individual",
    "Annual OOP max / entire famliy": "$13,100 family",
    "Eligible preventive care services": "100% covered, no deductible",
    "Primary care physician visits": 35,
    "Specialists": 50,
    "Centers of Excellence": "Not available",
    "Virtual doctor visits": "Not available",
    "Hospitalization": "75% covered after deductible",
    "Emergency": "75% covered after deductible",
    "Urgent care": "75% covered after deductible",
    "Generic drugs": 10,
    "Brand-name drugs": 50,
    "Speciality drugs": "See HMO provider"
  }
]

const array1Keys = [
  "employee Only",
  "employee + Spouse/Partner",
  "employee + Child(ren)",
  "employee + Family",
  " max contrib. / employee only",
  " max contrib. / employee + dependents",
  "Annual ded. / employee only",
  "Annual ded. / employee + dependents",
  "Annual OOP max / per person",
  "Annual OOP max / entire famliy"
]
const array2Keys = ["Primary care doctor visit",
  "Specialist doctor visit",
  "Virtual doctor visit",
  "Eligible preventive care",
  "Centers of Excellence",
  "Urgent care",
  "Emergency",
  "Hospitalization",
  "Generic drugs",
  "Brand-name drugs",
  "Speciality drugs"
]

const array1 = array1Keys.flatMap(key => input.map(obj => obj[key]))
const array2 = array2Keys.flatMap(key => input.map(obj => obj[key]))
console.log(array1)
console.log(array2)

相关问题