skip to Main Content

So I wrote this function to convert an object to an array.

Function:

function objectToArray(obj) {
    const result = [];
    
    for (const [key, value] of Object.entries(obj)) {
        if (typeof value === 'object' && value !== null) {
            result[key] = objectToArray(value);
        } else {
            result[key] = value;
        }
    }
    return result;
}

Object that I try 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)]

But when I try to convert the object that I want to convert it adds a lot of empty arrays and I don’t know why. Is this because there is something wrong with the Object or with my function?

Thanks for the help 😀

I have tried rewriting the function I provided multiple times but this is as close as I got to what I want.

2

Answers


  1. This is because you are not using the push functionality.

    result[key] = value; is essentially pushing to the array in that position.

    Instead you need:

    function objectToArray(obj) {
        const result = [];
        
        for (const [key, value] of Object.entries(obj)) {
            if (typeof value === 'object' && value !== null) {
                // push a spread of the array, this avoids nesting arrays
                result.push(objectToArray(value));
            } else {
                result.push(value);
            }
        }
        return result;
    }
    
    const initial = {
        "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"
                }
            ]
        }
    }
    
    console.log(objectToArray(initial))

    EDIT: Removed the spread operator to add depth

    Login or Signup to reply.
  2. You could take only the values and take flatMap for a flat result.

    function objectToArray(object) {
        return Object
            .values(object)
            .flatMap(value => value && typeof value === 'object'
                ? objectToArray(value)
                : value
            );
    }
    
    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" }] } };
    
    console.log(objectToArray(obj));
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search