I recently created a function to transform an object into an array.
Function:
function convertObjectToArray(obj) {
const result = [];
for (const [key, value] of Object.entries(obj)) {
if (typeof value === 'object' && value !== null) {
result[key] = convertObjectToArray(value);
} else {
result[key] = value;
}
}
return result;
}
The object I am attempting to convert:
const obj = {
"1129931367": {
"id": 10,
"amount": 1,
"assets": {
"appid": 252490,
"app_name": "name",
"classid": 1129931367,
"icon_url": "url",
"tradable": 1,
"name": "name",
"market_name": "market name",
"market_hash_name": "market hash name",
"sell_listings": 3215,
"sell_price": "0.10",
"updated_at": "17-Dec-2022"
},
"market_tradable_restriction": 7,
"market_marketable_restriction": 7,
"tags": [
{
"category": "category",
"internal_name": "internal name",
"localized_category_name": "localized category name",
"localized_tag_name": "localized tag name"
},
{
"category": "category",
"internal_name": "internal name",
"localized_category_name": "localized category name",
"localized_tag_name": "localized tag name"
}
]
}
}
Output:
(1129931368) [empty × 1129931367, Array(0)]
However, when I attempt to convert the desired object, it generates numerous empty arrays and I am unsure of the reason behind this. Could the issue lie within the Object or my function itself?
Thank you for any assistance provided!
I have made several attempts at revising the function, but this version is the closest I have come to achieving my goal.