I have the following multidimensional array and I want to get the smallest number and the largest number from the n_comp field and add both values to index 0 of each group in my array.
[
[
{
"l_grupo": "F003",
"total": 543
},
{
"d_anul": "0",
"c_comp": "01",
"n_seri": "F003",
"n_comp": "45",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "375.0000000"
},
{
"d_anul": "0",
"c_comp": "01",
"n_seri": "F003",
"n_comp": "46",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "52.0000000"
},
{
"d_anul": "0",
"c_comp": "01",
"n_seri": "F003",
"n_comp": "47",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "116.0000000"
}
],
[
{
"l_grupo": "B003",
"total": 406
},
{
"d_anul": "0",
"c_comp": "03",
"n_seri": "B003",
"n_comp": "21",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "35.0000000"
},
{
"d_anul": "0",
"c_comp": "03",
"n_seri": "B003",
"n_comp": "18",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "88.0000000"
},
{
"d_anul": "0",
"c_comp": "03",
"n_seri": "B003",
"n_comp": "15",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "93.0000000"
},
{
"d_anul": "0",
"c_comp": "03",
"n_seri": "B003",
"n_comp": "16",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "112.0000000"
},
{
"d_anul": "0",
"c_comp": "03",
"n_seri": "B003",
"n_comp": "17",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "78.0000000"
}
]
]
The result that I want to obtain is the following, I have tried to do it in many ways but I have not been able to achieve it.
[
[
{
"l_grupo": "F003",
"total": 543,
"desde": 45,
"hasta": 47
},
{
"d_anul": "0",
"c_comp": "01",
"n_seri": "F003",
"n_comp": "45",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "375.0000000"
},
{
"d_anul": "0",
"c_comp": "01",
"n_seri": "F003",
"n_comp": "46",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "52.0000000"
},
{
"d_anul": "0",
"c_comp": "01",
"n_seri": "F003",
"n_comp": "47",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "116.0000000"
}
],
[
{
"l_grupo": "B003",
"total": 406
"desde": 15,
"hasta": 21
},
{
"d_anul": "0",
"c_comp": "03",
"n_seri": "B003",
"n_comp": "21",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "35.0000000"
},
{
"d_anul": "0",
"c_comp": "03",
"n_seri": "B003",
"n_comp": "18",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "88.0000000"
},
{
"d_anul": "0",
"c_comp": "03",
"n_seri": "B003",
"n_comp": "15",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "93.0000000"
},
{
"d_anul": "0",
"c_comp": "03",
"n_seri": "B003",
"n_comp": "16",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "112.0000000"
},
{
"d_anul": "0",
"c_comp": "03",
"n_seri": "B003",
"n_comp": "17",
"f_comp": "2018-08-04 00:00:00.000",
"k_mone": "0",
"s_tipc": "1.0000000",
"s_tota": "78.0000000"
}
]
]
I hope someone can help me, thank you very much in advance and greetings.
With the help of going through all the elements and the nested elements except for element 1, we can save the values
n_comp
in an array to be able to use theMath.max.apply
orMath.min.apply
that returns the minimum or maximum value of a simple array. Check this for more information