I'm working with data returned from a 3rd party API and I really only need to retain certain properties from said objects. Each object is heavily nested with other objects contained within arrays, and occasionally a property that is just an array of strings. For instance, I get returned JSON such as:
{
itemId: '12345',
attributes: [ { attr1: ['red'], attr2: ['large', 'round'], attr3: ['obnoxious'] } ],
habits: [ 'bounce', 'roll', 'deflate' ]
}
This is just an example, but the real data is pretty messy and I would like to flatten everything down to a single object with no nesting and do it in the most efficient manner. I have experimented with several 'manual' ways of doing so with loops, but I feel like there must be some more efficient and resource-conscientious manner. I've looked through ES6-included methods of working with objects, but I can't seem to get the combination right.
The real object I'm working with:
{ itemId: [ '263010774375' ],
title: [ 'Star Wars Destiny Spirit of Rebellion - LEGENDARY' ],
globalId: [ 'EBAY-US' ],
primaryCategory: [ { categoryId: [Array], categoryName: [Array] } ],
galleryURL: [
'http://thumbs4.ebaystatic.com/pict/263010774375404000000004_1.jpg' ],
viewItemURL: [ 'http://www.ebay.com/itm/Star-Wars-Destiny-Spirit-Rebellion-
LEGENDARY-/263010774375?var=562018071609' ],
paymentMethod: [ 'PayPal' ],
autoPay: [ 'false' ],
postalCode: [ '78124' ],
location: [ 'Marion,TX,USA' ],
country: [ 'US' ],
shippingInfo:
[ { shippingServiceCost: [Array],
shippingType: [Array],
shipToLocations: [Array],
expeditedShipping: [Array],
oneDayShippingAvailable: [Array],
handlingTime: [Array] } ],
sellingStatus:
[ { currentPrice: [Array],
convertedCurrentPrice: [Array],
sellingState: [Array] } ],
listingInfo:
[ { bestOfferEnabled: [Array],
buyItNowAvailable: [Array],
startTime: [Array],
endTime: [Array],
listingType: [Array],
gift: [Array],
watchCount: [Array] } ],
returnsAccepted: [ 'true' ],
condition: [ { conditionId: [Array], conditionDisplayName: [Array] } ],
isMultiVariationListing: [ 'true' ],
topRatedListing: [ 'false' ] }`
The output I'd like to get would include bringing properties that are nested in arrays nested in other object properties that are also nested in arrays. My final object would only have one level, for example:
{
itemID: '12345',
title: 'something',
currentPrice: 20.00,
endTime: '2017-07-01',
condition: conditionDisplayName[0], // ex. 'Used'
location: 'Springfield, USA'
}