Reputation: 15571
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.
Upvotes: 1
Views: 115
Reputation: 5213
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 })
Upvotes: 1
Reputation: 519
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
}
}
]
Upvotes: 0
Reputation: 386848
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; }
Upvotes: 2