Js data structure reorganization

has the following data structure.


e   test1
b   test2
c   test2
i   test3
d   test3
f   test3
k   test3
h   test3

12


< hr >

:

[
    {
        category: "test1",
        arr: ["e"]
    },
    {
        category: "test2",
        arr: ["b","c"]
    },
    {
        category: "test3",
        arr: ["i","d","k","f","h"]
    }
]



[
    {
        category: "test1"
    },
    {
        category: "test2"
    },
    {
        category: "test3"
    }
].
Mar.10,2021

this is not difficult. You can create a new map, shape such as:

when you get the attribute array.
var cate = new Map()
cate.set('test1', 0)
cate.set('test2', 1)
cate.set('test3', 2)

then traverse the original array:

// 
prop = [
    {
        category: 'test1',
        arr: []
    },
    {
        category: 'test2',
        arr: []
    },
    {
        category: 'test3',
        arr: []
    }
]

// 
for (i in categorys) {
  prop[cate[categorys[i][1]].arr.push(lecategorys[i][0])
}
Menu