如何在JavaScript中编写以下php代码?

时间:2018-09-25 06:04:27

标签: javascript php jquery json

您好,我正在转换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"}];

4 个答案:

答案 0 :(得分:1)

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

var gst = [
  {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 result = gst.reduce(function (acc, ele) {
  var index = acc.findIndex(e => e.tax == ele.tax);
  if (index == -1) {
    acc.push({
      tax: ele.tax,
      percent: ele.percent
    })
  } else {
    acc[index].percent += parseFloat(ele.percent);
    acc[index].percent = acc[index].percent;
  }
  return acc;
}, []);

console.log(result);

答案 1 :(得分:0)

假设您在javascript(newResult)中有一个对象数组,则可以使用reduce()函数来生成out数组:

let newResult = [
   {
      tax: 'CGST@2.5%',
      percent: 2.38 
   },
   {
      tax: 'SGST@2.5%',
      percent: 2.38
   },
   {
      tax: 'CGST@2.5%',
      percent: 15.25
   },
   {
      tax: 'SGST@2.5%',
      percent: 3.57
   }
];

let out = newResult.reduce((acc, val) => {
   let existing = acc.filter((p) => p.tax === val.tax);
   if (existing.length === 1) {
      existing[0].percent += val.percent;
   } else {
      acc.push(val);
   }   
   return acc;
}, []);

console.log(out);

答案 2 :(得分:0)

或者您也可以使用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]});
}

答案 3 :(得分:0)

您在问题中提供的PHP输出与您提供的PHP代码不对应。该代码将产生一个关联数组,其中键是税码,值将是逗号分隔的字符串。我将假定代码是正确的,而PHP输出是错误的。

在JavaScript中,最好将PHP关联数组转换为普通对象(或者在ES6中,可以使用Map)。对于一个普通对象,实际上是向JavaScript的转换看起来像这样:

const newResult = [
    { 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 }
];

const out = {}; // <== object
for (const value of newResult) {
    if (!(value.tax in out)) {
       out[value.tax] = Object.assign({}, value); // This copies all property values
    } else {
        out[value.tax].percent += ", " + value.percent; // Converts number to string
    }
}

console.log(out);

我个人更喜欢在有多个匹配百分比时将percent值创建为数组而不是逗号分隔的字符串,但是由于您已经在PHP中做出了选择,因此我也像在JS中一样。我发现这样做的问题是,您留下了percent值,这些值有时是数字(当没有聚合时),有时是字符串(当是多值时)。再说一次,就像您在PHP中一样。

相关问题