2 votes

Comment diviser un tableau en fonction de la valeur d'une propriété sans changer l'ordre des clés ?

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.

0voto

eugen sunic Points 2937
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"
  }
]

const result = test_array.reduce((acc, x) => ({
...acc,
[x.header_region]: [...(acc[x.header_region] || []), x]
}), {});

console.log(result)

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X