1

I have an array like this:

[{
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
}]

And I need it like this:

[{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
}]

Please help.

Earlier I wrote a function to convert flat navigation array to a tree, but that does not help here.

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

So, I'm looking for a fresh direction.

4
  • There are no multi level arrays here. Commented Aug 29, 2016 at 11:56
  • @Vld, you are right. Possibly I am not being able to put the words in properly here. Commented Aug 29, 2016 at 11:58
  • Do you need to group them by iJobType property? Commented Aug 29, 2016 at 12:00
  • Right @Bommox but no summary. Commented Aug 29, 2016 at 12:10

3 Answers 3

2

You could use a hash table as reference for the value of iJobType and build an array upon of this.

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

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

1 Comment

Good thinking to display the console at max height.
1

If the requirement is for searching or filtering purpose, changing the structure of the data is not required. You can use the filter method to obtain the elements having a given iJobType value:

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

Comments

0

A simple function to create the new data array

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

Output:

[{
    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
        }
    }
]   

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.