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.
asked Aug 29, 2016 at 11:54
Kangkan
15.6k11 gold badges79 silver badges116 bronze badges
-
There are no multi level arrays here.VLAZ– VLAZ2016年08月29日 11:56:37 +00:00Commented Aug 29, 2016 at 11:56
-
@Vld, you are right. Possibly I am not being able to put the words in properly here.Kangkan– Kangkan2016年08月29日 11:58:45 +00:00Commented Aug 29, 2016 at 11:58
-
Do you need to group them by iJobType property?Jorgeblom– Jorgeblom2016年08月29日 12:00:31 +00:00Commented Aug 29, 2016 at 12:00
-
Right @Bommox but no summary.Kangkan– Kangkan2016年08月29日 12:10:42 +00:00Commented Aug 29, 2016 at 12:10
3 Answers 3
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; }
answered Aug 29, 2016 at 12:01
Nina Scholz
388k26 gold badges367 silver badges417 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Redu
Good thinking to display the console at max height.
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 })
answered Aug 29, 2016 at 12:20
Alberto De Caro
5,2139 gold badges51 silver badges77 bronze badges
Comments
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
lang-js