J'ai un tableau comme :
var test_array = [
{
"test_name":"vna",
"header_region":"vna_results",
"header_name":"larm",
"value_title":"area"},
{
"test_name":"vna",
"header_region":"vna_results",
"header_name":"larm",
"value_title":"area"},
{
"test_name":"lma",
"header_region":"lma_results",
"header_name":"larm",
"value_title":"area"},
{
"test_name":"lma",
"header_region":"lma_results",
"header_name":"larm",
"value_title":"area"},
{
"test_name":"uni",
"header_region":"uni_results",
"header_name":"larm",
"value_title":"area"},
{
"test_name":"uni",
"header_region":"uni_results",
"header_name":"larm",
"value_title":"area"}
]
et je veux le diviser en objets en fonction de la valeur de la propriété : header_name.
{
"vna_results":[
{
"test_name":"vna",
"header_region":"vna_results",
"header_name":"larm",
"value_title":"area"
},
{
"test_name":"vna",
"header_region":"vna_results",
"header_name":"larm",
"value_title":"area"
}
],
"lma_results":[
{
"test_name":"vna",
"header_region":"lma_results",
"header_name":"larm",
"value_title":"area"
},
{
"test_name":"vna",
"header_region":"lma_results",
"header_name":"larm",
"value_title":"area"
}
],
"uni_results":[
{
"test_name":"vna",
"header_region":"uni_results",
"header_name":"larm",
"value_title":"area"
},
{
"test_name":"vna",
"header_region":"uni_results",
"header_name":"larm",
"value_title":"area"
}
]
}
J'utilise la fonction ci-dessous pour y parvenir :
function groupBy(arr, property) {
return arr.reduce(function(memo, x) {
if (!memo[x[property]]) { memo[x[property]] = []; }
memo[x[property]].push(x);
return memo;
}, {});
}
var splitted = groupBy(test_array, 'header_region');
but the output result changed the order of keys: lma_results first then uni and then vna..
lmi_results:{...},
uni_results:{...},
vna_results:{...},
expected order- same as in array
i don't want to change the order. how to achieve this. Thanks in advance.