Saya mencoba mengubah objek ini:

[
    {
        "keyword":"apple",
        "category_1":"object",
        "category_2":"living",
        "category_3":"fruit",
        "count":5
    },
    {
        "keyword":"orange",
        "category_1":"object",
        "category_2":"living",
        "category_3":"fruit",
        "count":1
    },
    {
        "keyword":"table",
        "category_1":"object",
        "category_2":"non living",
        "category_3":"house item",
        "count":3
    },
    {
        "keyword":"cat",
        "category_1":"object",
        "category_2":"living",
        "category_3":"animal",
        "count":4
    },
    {
        "keyword":"stadium",
        "category_1":"place",
        "category_2":"sport related",
        "category_3":"indoor",
        "count":2
    }
]

Menjadi objek seperti ini:

[
    {
        label: 'object',
        count: 9,
        childs: [
            {
                label: 'living',
                count: 6,
                childs: [
                    {
                        label: 'fruit',
                        count: 6,
                        childs: [
                            {
                                keyword: 'apple',
                                count: 5,
                            },
                            {
                                keyword: 'orange',
                                count: 1,
                            }
                        ]
                    }
                ]
            },
            {
                label: 'non living',
                count: 3,
                childs: [
                    {
                        label: 'animal',
                        count: 3,
                        childs: [
                            {
                                keyword: 'cat',
                                count: 3,
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        label: 'place',
        count: 2,
        childs: [
            {
                label: 'sport related',
                count: 2,
                childs: [
                    {
                        label: 'indoor',
                        count: 2,
                        childs: [
                            {
                                keyword: 'stadium',
                                count: 2,
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

Saya mencoba dengan Array.reduce secara rekursif, tetapi saya memiliki masalah dengan rekursif, saya selalu berakhir menabrak dinding. Seperti yang Anda lihat, intinya adalah mengubah array menjadi objek bersarang yang mengelompokkan elemen-elemennya berdasarkan kategori (bagian hitungan tidak penting)

Jika ada yang punya pandangan ke belakang tentang ini

1
Gatoyu 28 Oktober 2019, 14:18

1 menjawab

Jawaban Terbaik

Anda dapat terlebih dahulu mengubah data menjadi Peta bersarang, masing-masing dikunci oleh kategori yang relevan. Ini memungkinkan identifikasi cepat dari cabang yang benar tempat untuk menyuntikkan objek berikutnya.

Kemudian terapkan fungsi rekursif untuk mengubah pohon berbasis Peta itu menjadi struktur target, sambil meningkatkan jumlah.

Kode:

function makeTree(data) {
    // Create tree using nested Maps, keyed by category
    let main = new Map;
    for (let {keyword, category_1, category_2, category_3, count} of data) {
        let obj = { keyword, count };
        let map = main;
        for (let cat of [category_1, category_2, category_3]) {
            let child = map.get(cat);
            if (!child) map.set(cat, child = new Map);
            map = child;
        }
        let child  = map.get(keyword);
        if (!child) map.set(keyword, child = { keyword, count: 0 });
        child.count +=  count;
    }

    // Recursive function to turn the nested Maps into the target structure
    function transform(data) {
        let parentCount = 0;
        let arr = Array.from(data, ([label, value]) => {
            let count, children;
            if (value instanceof Map) {
                ([children, count] = transform(value));
                value = { label, count, children };
            } else {
                ({ count } = value);
            }
            parentCount += count;
            return value;        
        });
        return [arr, parentCount];
    }
    return transform(main)[0];
}

// Example run:
let data = [{"keyword":"apple","category_1":"object","category_2":"living","category_3":"fruit","count":5},{"keyword":"orange","category_1":"object","category_2":"living","category_3":"fruit","count":1},{"keyword":"table","category_1":"object","category_2":"non living","category_3":"house item","count":3},{"keyword":"cat","category_1":"object","category_2":"living","category_3":"animal","count":4},{"keyword":"stadium","category_1":"place","category_2":"sport related","category_3":"indoor","count":2}];

console.log(makeTree(data));
2
trincot 28 Oktober 2019, 11:47