Javascript数组检查值并分配到数组中

时间:2016-08-05 14:21:29

标签: javascript angularjs arrays

所以,我有一个PHP函数,它将读取一个csv文件,然后将其传递给angularJS。

在csv文件中,有4种类型的数据

[0] => Number,
[1] => Code,
[2] => Description,
[3] => Qty

但是,还有“区域”数据。 “区域”将在“数字”[2]

[0] => "",
[1] => "",
[2] => Area,
[3] => ""

这是插图

//Part 1
"","","Area 1",""
"No","Code","Description","QTY"
"No","Code","Description","QTY"
"No","Code","Description","QTY"
"No","Code","Description","QTY"
"No","Code","Description","QTY"
//Part 2
"","","Area 2",""
"No","Code","Description","QTY"
"No","Code","Description","QTY"
"No","Code","Description","QTY"
"No","Code","Description","QTY"
//And so on

我想要实现的是,如何通过检查[0]是否为空并且[2]不为空来获取该区域然后将其推入我的数组,那么它是其余数组对象的区域,一旦它检测到与以前相同的条件,它将为另一部分分配新区域。

这是我的阵列,名为vm.products

for (var index = 0; index < resp.data.csv.length; index++) {
   vm.products.push({
      code: resp.data.csv[index][1],
      qty: resp.data.csv[index][3], 
      //area: resp.data.csv[index].area,
      description: resp.data.csv[index][2]
   });
}

感谢您给予的任何帮助:)

3 个答案:

答案 0 :(得分:1)

首先,您必须检查当前条目是否包含Area。如果是这样,请将其存储到下一个区域。当发生这种情况时,只需用新的区域替换旧区域。

在将产品推送到数组时,只需使用Area变量,因为它始终包含正确的区域。

var area = “”;
for (var index = 0; index < resp.data.csv.length; index++) {
    var current = resp.data.csv[index];
    if (current[2] != “” && (!current[0] || current[0] == "") {
        area = current[2];
    } else {
        vm.products.push({
            code: current[1],
            qty: current[3], 
            area: area,
            description: current[2]
        });
    }
}

此解决方案仅在您的阵列上循环一次,而不是在下面的答案中首先使用地图时多次循环。

答案 1 :(得分:1)

您可以只映射和过滤数据:

var area;
var res = arr.map(function (d) {
    if (d[0] == "" && d[2] != null) {
        area = d[2];
        return null;
    }

    return {
        code: d[1],
        qty: d[3],
        area: area,
        description: d[2]
    }

}).filter(function (f) {
    return f;
});

console.log(res);

样本数据的输出:

[ { code: 'Code',
    qty: 'QTY',
    area: 'Area 1',
    description: 'Description' },
  { code: 'Code',
    qty: 'QTY',
    area: 'Area 1',
    description: 'Description' },
  { code: 'Code',
    qty: 'QTY',
    area: 'Area 1',
    description: 'Description' },
  { code: 'Code',
    qty: 'QTY',
    area: 'Area 1',
    description: 'Description' },
  { code: 'Code',
    qty: 'QTY',
    area: 'Area 1',
    description: 'Description' },
  { code: 'Code',
    qty: 'QTY',
    area: 'Area 2',
    description: 'Description' },
  { code: 'Code',
    qty: 'QTY',
    area: 'Area 2',
    description: 'Description' },
  { code: 'Code',
    qty: 'QTY',
    area: 'Area 2',
    description: 'Description' },
  { code: 'Code',
    qty: 'QTY',
    area: 'Area 2',
    description: 'Description' } ]

答案 2 :(得分:0)

你可能会做这样的事情;

&#13;
&#13;
var csvStr = '"","","Area 1",""\n"No","Code","Description","QTY"\n"No","Code","Description","QTY"\n"No","Code","Description","QTY"\n"No","Code","Description","QTY"\n"No","Code","Description","QTY"\n"","","Area 2",""\n"No","Code","Description","QTY"\n"No","Code","Description","QTY"\n"No","Code","Description","QTY"\n"No","Code","Description","QTY"',
csvDataArr = csvStr.replace(/"/g,'').split("\n").map(s => s.split(",")),
      area,
csvDataObj = csvDataArr.reduce((p,c) => {c[0] === "" &&
                                         c[1] === "" &&
                                         c[3] === ""  ? p[area = c[2]] = []
                                                      : p[area].push(c);
                                         return p;
                                        },{});
console.log(csvDataObj);
&#13;
&#13;
&#13;