0

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 :D

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

2
  • 3
    What is the expected output? result is an array and you are updating result[key] instead of pushing into the array. When you add the index 1129931367, the array length becomes 1129931368. Commented Dec 23, 2022 at 15:41
  • "convert an object to an array" this premise doesn't really make much sense. There is no apparent reason to do that. The object is obviously not representable by an array. So, the conversion is just taking an object and smashing all its properties to an array which...gives you a worse object at the end. What are you really after, since what you have right now seems just like an XY problem? Commented Dec 23, 2022 at 15:45

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

This makes an array of every key and value from the object. I want it so it keeps the depth /nesting from the object
@JasperErkel no problem, see edit, I have removed the spread operator and now the flattening had been removed
So close but I would like to keep the keys from the object. So you would get somethin glike this: "1129931367": [ "id": 10, "amount": 1, "assets": [ etc,
0

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.