Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

Convert Object To Array Generic Function

node v14.20.1
version: 1.0.0
endpointsharetweet
const data1 = [{ "id": "9997602-1", "name": "Choose 1", "numberOfChoices": 1, "dealItemVariations": [{ "dealItemVariationId": "9997607", "minChoices": 0, "maxChoices": 1, "additionPrice": 0, "nutritionalInfo": null, "numberOfServings": null }, { "dealItemVariationId": "9997604", "minChoices": 0, "maxChoices": 1, "additionPrice": 0, "nutritionalInfo": null, "numberOfServings": null }, { "dealItemVariationId": "9997603", "minChoices": 0, "maxChoices": 1, "additionPrice": 0, "nutritionalInfo": null, "numberOfServings": null }, { "dealItemVariationId": "9997606", "minChoices": 0, "maxChoices": 1, "additionPrice": 0, "nutritionalInfo": null, "numberOfServings": null }] }, { "id": "9997602-2", "name": "Choose 1", "numberOfChoices": 1, "dealItemVariations": [{ "dealItemVariationId": "9997611", "minChoices": 0, "maxChoices": 1, "additionPrice": 0, "nutritionalInfo": null, "numberOfServings": null }, { "dealItemVariationId": "9997610", "minChoices": 0, "maxChoices": 1, "additionPrice": 0, "nutritionalInfo": null, "numberOfServings": null }, { "dealItemVariationId": "9997609", "minChoices": 0, "maxChoices": 1, "additionPrice": 0, "nutritionalInfo": null, "numberOfServings": null }] }, { "id": "9997602-3", "name": "Choose 1", "numberOfChoices": 1, "dealItemVariations": [{ "dealItemVariationId": "9997586", "minChoices": 0, "maxChoices": 1, "additionPrice": 0, "nutritionalInfo": null, "numberOfServings": null }, { "dealItemVariationId": "9997585", "minChoices": 0, "maxChoices": 1, "additionPrice": 0, "nutritionalInfo": null, "numberOfServings": null }] }] const data2 = [ "02f8d2ed-da5b-53c7-994f-83d50d185467", "0ce3911e-e633-500b-a6e2-3079f59669c7", "18dd9e3f-ecfa-57f7-97b4-7128125ee1b7", "1e32f0f6-1f31-5506-85f0-f6a00b5faacd", "1f506d82-e420-530d-b0a5-f06c03897a15" ]
const convertArrayToObject = (arr = [], keyFn) => arr .reduce((obj, i) => { obj[keyFn(i)] = i return obj }, {})
const data1Output = convertArrayToObject(data1, i => i.id)
const data2Output = convertArrayToObject(data2, i => i)
Loading…

no comments

    sign in to comment