js flat array to multi-level array

时间:2016-08-29 11:54:27

标签: javascript arrays

我有一个这样的数组:

[{
iStatusId: 4,
vStatus: "Under Preparation",
iJobType: 1,
bIsActive: true,
iOrder: 2
},
{
iStatusId: 5,
vStatus: "Stamp & Signatures by Client",
iJobType: 1,
bIsActive: true,
iOrder: 3
},
{
iStatusId: 7,
vStatus: "CA & CE Certification",
iJobType: 1,
bIsActive: true,
iOrder: 6
},
{
iStatusId: 8,
vStatus: "Application Submission Date",
iJobType: 1,
bIsActive: true,
iOrder: 4
},
{
iStatusId: 9,
vStatus: "File in HQ-BRU/Tech ",
iJobType: 1,
bIsActive: true,
iOrder: 7
}]

我需要这样:

[{iJobType: 1, data:
{
iStatusId: 4,
vStatus: "Under Preparation",
bIsActive: true,
iOrder: 2
},
{
iStatusId: 5,
vStatus: "Stamp & Signatures by Client",
bIsActive: true,
iOrder: 3
},
{
iStatusId: 7,
vStatus: "CA & CE Certification",
bIsActive: true,
iOrder: 6
},
{
iStatusId: 8,
vStatus: "Application Submission Date",
bIsActive: true,
iOrder: 4
},
{
iStatusId: 9,
vStatus: "File in HQ-BRU/Tech ",
verification",
bIsActive: true,
iOrder: 7
}]

请帮忙。

之前我写过一个将平面导航阵列转换为树的函数,但这在这里没有用。

$scope.navConvert = function(array) {
    var map = {};
    for (var i = 0; i < array.length; i++) {
        var obj = array[i];
        obj.items = [];

        map[obj.NavId] = obj;

        var parent = obj.NavParent || '-';
        if (!map[parent]) {
            map[parent] = {
                items: []
            };
        }
        map[parent].items.push(obj);
    }
    return map['-'].items;
}

所以,我正在寻找新的方向。

3 个答案:

答案 0 :(得分:2)

您可以使用哈希表作为iJobType值的参考,并在此基础上构建数组。

var data = [{ iStatusId: 4, vStatus: "Under Preparation", iJobType: 1, bIsActive: true, iOrder: 2 }, { iStatusId: 5, vStatus: "Stamp & Signatures by Client", iJobType: 1, bIsActive: true, iOrder: 3 }, { iStatusId: 7, vStatus: "CA & CE Certification", iJobType: 1, bIsActive: true, iOrder: 6 }, { iStatusId: 8, vStatus: "Application Submission Date", iJobType: 1, bIsActive: true, iOrder: 4 }, { iStatusId: 9, vStatus: "File in HQ-BRU/Tech ", iJobType: 1, bIsActive: true, iOrder: 7 }],
    grouped = [];

data.forEach(function (a) {
    if (!this[a.iJobType]) {
        this[a.iJobType] = { iJobType: a.iJobType, data: [] };
        grouped.push(this[a.iJobType]);
    }
    this[a.iJobType].data.push({
        iStatusId: a.iStatusId,
        vStatus: a.vStatus,
        bIsActive: a.bIsActive,
        iOrder: a.iOrder
    });
}, Object.create(null));

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

如果要求用于搜索或过滤目的,则不需要更改数据结构。 您可以使用filter方法获取具有给定iJobType值的元素:

var arr = [{
iStatusId: 4,
vStatus: "Under Preparation",
iJobType: 1,
bIsActive: true,
iOrder: 2
},
{
iStatusId: 5,
vStatus: "Stamp & Signatures by Client",
iJobType: 1,
bIsActive: true,
iOrder: 3
},
{
iStatusId: 7,
vStatus: "CA & CE Certification",
iJobType: 1,
bIsActive: true,
iOrder: 6
},
{
iStatusId: 8,
vStatus: "Application Submission Date",
iJobType: 1,
bIsActive: true,
iOrder: 4
},
{
iStatusId: 9,
vStatus: "File in HQ-BRU/Tech ",
iJobType: 1,
bIsActive: true,
iOrder: 7
}];
arr

//1. define the filter function
function filterByJobType(value){
  //this.filterValue will be passed as a parameter in the filter execution
  return value.iJobType == this.jobTypeFilter
}

//2. res is the subset of elements from arr fullfilling the filter condition
var res = arr.filter(filterByJobType, {jobTypeFilter: 1 })

答案 2 :(得分:0)

创建新数据数组的简单函数

var data = [{
        iStatusId: 4,
        vStatus: "Under Preparation",
        iJobType: 1,
        bIsActive: true,
        iOrder: 2
    },
    {
        iStatusId: 5,
        vStatus: "Stamp & Signatures by Client",
        iJobType: 1,
        bIsActive: true,
        iOrder: 3
    }
]   
var newData = []
for (var i in data) {
    var tempObj = {iJobType: data[i].iJobType, data: data[i]};
    delete tempObj.data.iJobType;
    newData.push(tempObj)
}

输出:

[{
    iJobType: 1,
        data: {
            iStatusId: 4,
            vStatus: "Under Preparation",
            bIsActive: true,
            iOrder: 2
        }
    },
    {
        iJobType: 1,
        data: {
            iStatusId: 5,
            vStatus: "Stamp & Signatures by Client",
            bIsActive: true,
            iOrder: 3
        }
    }
]