Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 16030

Transform object key names with numbers into array

$
0
0

I have this object:

{'categories[0].alias': 'gyms','categories[0].title': 'Gyms','categories[1].alias': 'healthtrainers',       'categories[1].title': 'Trainers','location.display_address[0]': '360 Hampton Dr','location.display_address[1]': 'Venice, CA 90291'}

... and am trying to transform it into this:

{    categories: [        { alias: 'gyms', title: 'Gyms' },        { alias: 'healthtrainers', title: 'Trainers' }    ],    location: {        display_address: [ '360 Hampton Dr', 'Venice, CA 90291' ]    }}

Both of these [1, 2] answers are treating the number as a string and leaving it in the name:

{"categories[0]" : {"alias": "gyms","title": "Gyms"    },"categories[1]": {"alias": "healthtrainers","title": "Trainers"    },"location":{"display_address[0]": "360 Hampton Dr","display_address[1]": "Venice, CA 90291"    }}

I'd like to keep the existing functionality of either answer, but handle keys containing an array position properly.


Viewing all articles
Browse latest Browse all 16030

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>