简体   繁体   English

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

[英]Javascript array check value and assign into the array

So, I have a PHP function that will read a csv file and then pass it to the angularJS. 因此,我有一个PHP函数,该函数将读取一个csv文件,然后将其传递给angularJS。

Inside the csv file, there are 4 types of data 在csv文件中,有4种数据类型

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

However, there is also "Area" data. 但是,也有“面积”数据。 The "Area" will be on "number" [2] “区域”将在“数字”上[2]

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

This is the illustration 这是插图

//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

What I want to achieve is, how can I get the area and then push it into my array by checking if [0] is empty and [2] is not null, then it is the area for the rest of array object, and once it detects same condition as before, it will assign new area for another part. 我想要实现的是,如何获取区域,然后通过检查[0]是否为空和[2]不为空,然后将其推入数组,则该区域为其余数组对象的区域,一次它检测到与以前相同的条件,它将为另一部分分配新区域。

This is my array, called vm.products 这是我的数组,称为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]
   });
}

Thank you for any help given :) 谢谢您给予的任何帮助:)

First, you have to check whether the current entry contains the Area. 首先,您必须检查当前条目是否包含区域。 If so, store it until the next Area comes by. 如果是这样,请将其存储到下一个区域通过。 When that happens, just replace the old Area with the new one. 发生这种情况时,只需用新区域替换旧区域。

While pushing the products to the array, just use the Area variable as it will always contain the correct Area. 将产品推入阵列时,只需使用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]
        });
    }
}

This solution will loop only once over your array instead of multiple times when using a map first in the answer below. 此解决方案将仅在您的数组上循环一次,而不是在下面的答案中首先使用地图时多次循环。

You could just map and filter the data: 您可以映射并过滤数据:

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);

Output for your sample data: 输出样本数据:

[ { 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' } ]

You might do something like this; 您可能会做这样的事情;

 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM