1

I have this JSON structure, I want to create 2 arrays out of this array of JSON. one array having all the values from key "employee only" to ""Annual OOP max / entire famliy" (from each object in JSON array) and 2nd array to have values from key "Primary care doctor visit" to end of the object".

array 1 = [86.1,30.5,33.2, 291.3, 154.1, 121.2..............13700, "$13,100 family"] (so for eg "employee only" key from object1 then object2 then till last object, again next key from object 1...till last object)

similarly in array 2 starting from key "Primary care doctor visit" till end of object

array 2 = ["75% covered after deductible", "$35 copay",35,............, "See HMO provider"]

How can i achieve this in 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"
  }
]

6
  • Please provide a minimal reproducable example Commented May 11, 2021 at 14:03
  • I am not getting how do i do this..nowhere i could get the reference to iterate in this way Commented May 11, 2021 at 14:06
  • Create an Array with some of the values, check something like Object.entries to explore the Objects within the Array and play around with it. Commented May 11, 2021 at 14:12
  • Does this answer your question? How to create a new array out of array of objects? Commented May 11, 2021 at 16:39
  • I don't really see what is the difference in your questions. Please don't post duplicate questions like that and if there is a difference then refer to your previous question and explain what is different Commented May 11, 2021 at 16:40

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.