Hi i am working on converting a php application to continue billing even internet is not working. So i am converting the php codes to javascript. In between i got stuck at an area where my billing ends. Here i need to group the similar tax % to one. Can someone let me know how can it be done ?
$new_result = Array
(
[0] => Array
(
[tax] => [email protected]%
[percent] => 2.38
)
[1] => Array
(
[tax] => [email protected]%
[percent] => 2.38
)
[2] => Array
(
[tax] => CGST@9%
[percent] => 15.25
)
[3] => Array
(
[tax] => SGST@9%
[percent] => 15.25
)
[4] => Array
(
[tax] => [email protected]%
[percent] => 3.57
)
[5] => Array
(
[tax] => [email protected]%
[percent] => 3.57
)
)
$out = array();
foreach ($new_result as $key => $value){
if (array_key_exists($value['tax'], $out)){
$out[$value['tax']]['percent'] += ', '+$value['percent'];
} else {
$out[$value['tax']] = array('tax' => $value['tax'], 'percent' => $value['percent']);
}
}
Output :
Array
(
[0] => Array
(
[tax] => [email protected]%
[percent] => 5.95
)
[1] => Array
(
[tax] => [email protected]%
[percent] => 5.95
)
[2] => Array
(
[tax] => CGST@9%
[percent] => 15.25
)
[3] => Array
(
[tax] => SGST@9%
[percent] => 15.25
)
)
My try :
var out = [];
$.each(new_result, function(key,value5) {
if(value5.tax in out){
//
}else{
out[value5.tax] = [{tax:value5.tax, percent:value5.percent}];
}
});
Input array
var newresult = [{"tax":"CGST@9%","percent":"76.27"},{"tax":"SGST@9%","percent":"76.27"},{"tax":"CGST@9%","percent":"15.25"},{"tax":"SGST@9%","percent":"15.25"},{"tax":"[email protected]%","percent":"3.57"},{"tax":"[email protected]%","percent":"3.57"}];