json 对象到数组错误

7fyelxc5  于 2022-12-24  发布在  其他
关注(0)|答案(2)|浏览(115)

所以我写了这个函数把一个对象转换成一个数组。
功能:

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

我尝试转换的对象:

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"
            }
        ]
    }
}

输出:

(1129931368) [empty × 1129931367, Array(0)]

但是当我试图转换我想要转换的对象时,它添加了很多空数组,我不知道为什么,这是因为对象或者我的函数有问题吗?
谢谢你的帮助:D
我已经尝试过多次重写我提供的函数,但这已经接近我想要的了。

rdlzhqv9

rdlzhqv91#

这是因为您没有使用push功能。
result[key] = value;实际上是在那个位置推到阵列。
相反,您需要:

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))
fdbelqdn

fdbelqdn2#

您可以只获取值,然后获取flatMap以获得平面结果。

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

相关问题