如何用javascript编写下面的php代码。?

如何用javascript编写下面的php代码。?,javascript,php,jquery,json,Javascript,Php,Jquery,Json,嗨,我正在转换一个php应用程序,以继续计费,即使互联网不工作。因此,我将php代码转换为javascript。在这两者之间,我被困在一个我的账单结束的地方。在这里,我需要将类似的税%归为一。有人能告诉我怎么做吗 $new_result = Array ( [0] => Array ( [tax] => SGST@2.5% [percent] => 2.38 ) [1] =>

嗨,我正在转换一个php应用程序,以继续计费,即使互联网不工作。因此,我将php代码转换为javascript。在这两者之间,我被困在一个我的账单结束的地方。在这里,我需要将类似的税%归为一。有人能告诉我怎么做吗

$new_result = Array
(
    [0] => Array
        (
            [tax] => SGST@2.5%
            [percent] => 2.38
        )

    [1] => Array
        (
            [tax] => CGST@2.5%
            [percent] => 2.38
        )

    [2] => Array
        (
            [tax] => CGST@9%
            [percent] => 15.25
        )

    [3] => Array
        (
            [tax] => SGST@9%
            [percent] => 15.25
        )

    [4] => Array
        (
            [tax] => SGST@2.5%
            [percent] => 3.57
        )

    [5] => Array
        (
            [tax] => CGST@2.5%
            [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']);
            }
        }
输出:

Array
(
    [0] => Array
        (
            [tax] => SGST@2.5%
            [percent] => 5.95
        )

    [1] => Array
        (
            [tax] => CGST@2.5%
            [percent] => 5.95
        )

    [2] => Array
        (
            [tax] => CGST@9%
            [percent] => 15.25
        )

    [3] => Array
        (
            [tax] => SGST@9%
            [percent] => 15.25
        )

)
我的尝试:

    var out = [];
    $.each(new_result, function(key,value5) {
        if(value5.tax in out){
            //
        }else{
            out[value5.tax] = [{tax:value5.tax, percent:value5.percent}];
        }
    });
输入阵列

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":"SGST@2.5%","percent":"3.57"},{"tax":"CGST@2.5%","percent":"3.57"}];

在javascript中,您可以使用下面的
reduce
函数进行分组

var gst=[
{税务:'SGST@2.5%,百分比:2.38},
{税务:'CGST@2.5%,百分比:2.38},
{税务:'CGST@9%,百分比:15.25},
{税务:'SGST@9%,百分比:15.25},
{税务:'SGST@2.5%,百分比:3.57},
{税务:'CGST@2.5%“,百分比:3.57}
];
var结果=gst.减少(功能(acc,ele){
var指数=acc.findIndex(e=>e.tax==ele.tax);
如果(索引==-1){
加速推({
税务:电子税务,
百分比:ele.percent
})
}否则{
acc[索引]。百分比+=浮动(元素百分比);
acc[索引]。百分比=acc[索引]。百分比;
}
返回acc;
}, []);

控制台日志(结果)
假设您在javascript中有一个对象数组(
newResult
),您可以使用
reduce()
函数生成
out
数组:

让newResult=[
{
税:'CGST@2.5%',
百分比:2.38
},
{
税:'SGST@2.5%',
百分比:2.38
},
{
税:'CGST@2.5%',
百分比:15.25
},
{
税:'SGST@2.5%',
百分比:3.57
}
];
释放=新结果。减少((acc,val)=>{
let existing=acc.filter((p)=>p.tax==val.tax);
if(existing.length==1){
现有[0]。百分比+=val.percent;
}否则{
加速推力(val);
}   
返回acc;
}, []);

控制台。注销或者您也可以使用JS执行相同的操作

    var myArray = [
  {tax: 'SGST@2.5%', percent: 2.38},
  {tax: 'CGST@2.5%', percent: 2.38},
  {tax: 'CGST@9%', percent: 15.25},
  {tax: 'SGST@9%', percent: 15.25},
  {tax: 'SGST@2.5%', percent: 3.57},
  {tax: 'CGST@2.5%', percent: 3.57}
];

var groups = {};
for (var i = 0; i < myArray.length; i++) {
  var groupName = myArray[i].tax;
  if (!groups[groupName]) {
    groups[groupName] = [];
  }
  groups[groupName].push(myArray[i].percent);
}
myArray = [];
for (var groupName in groups) {
  myArray.push({group: groupName, tax: groups[groupName]});
}
var myArray=[
{税务:'SGST@2.5%,百分比:2.38},
{税务:'CGST@2.5%,百分比:2.38},
{税务:'CGST@9%,百分比:15.25},
{税务:'SGST@9%,百分比:15.25},
{税务:'SGST@2.5%,百分比:3.57},
{税务:'CGST@2.5%“,百分比:3.57}
];
变量组={};
对于(var i=0;i
您在问题中给出的PHP输出与您给出的PHP代码不一致。该代码将生成一个关联数组,其中键是税码,值是逗号分隔的字符串。我假设代码是正确的,而PHP输出是错误的

在JavaScript中,最好将PHP关联数组转换为普通对象(或者在ES6中,可以使用
Map
)。对于普通对象,相当字面的JavaScript转换如下所示:

const newResult=[
{税务:'SGST@2.5%,百分比:2.38},
{税务:'CGST@2.5%,百分比:2.38},
{税务:'CGST@9%,百分比:15.25},
{税务:'SGST@9%,百分比:15.25},
{税务:'SGST@2.5%,百分比:3.57},
{税务:'CGST@2.5%“,百分比:3.57}
];

const out={};//我想根据百分比值对它们进行分组。坦白地说,我不知道那个php代码片段。它是由以前的开发人员完成的。您提供的PHP输出与您提供的PHP代码不一致。它将生成一个关联数组,而不是一个带有数字索引的数组。此外,PHP将连接百分比,而百分比在输出中不存在。你能修改你的输出吗?输出是我打印时得到的,你的答案以逗号分隔。当我不断添加同一税务组的3种产品时,此代码无法添加百分比。@SarathHari,你可以用你面临的问题发布不同的问题,然后所有人和我都可以看到;-)但问题是在同一个代码中bro@JavascriptLover sktuncapt TypeError:acc[index].percent.toFixed不是一个函数今天是这个项目提交,我仍然困在办公室里