从大型json数据集创建对象数组

时间:2018-06-02 18:45:42

标签: javascript arrays object mapreduce

我正在使用AngularJS构建我的第一个更大的应用程序,并且难以构建用于绘制数据的对象数组。目前,我正在使用Excel文件并使用SheetJS创建JSON数据。 存储在变量" jsonData"中的大约有40,000个条目。格式为:

Console Printout of JSON

我的最终目标是绘制"关闭金额"每个航空公司每个月。我的想法是创建一个对象数组,用于存储航空公司名称和一系列索赔数据(称为airlineData)。我很难循环遍历jsonData,根据if语句将所需信息添加到对象数组中,以查看是否已添加该航空公司名称。

我在github上有代码 - https://github.com/mikebly/tsa-luggage-analysis

为简单起见,这是我通过jsonData的整个循环:

for(i = 0; i < jsonData.length; i++){

    var date = XLSX.SSF.parse_date_code(jsonData[i]["Incident Date"],{date1904:false});
    var month = date.m; // Returns 1,2,3,...,12
    var airport = String(jsonData[i]["Airport Code"]);
    var airline = String(jsonData[i]["Airline Name"]);
    var claim = Number(jsonData[i]["Close Amount"]);
    claim = claim || 0; // Convert "-" to 0 for summing and average
    claimTotal += claim;

    // Build airport data array of objects to keep track of each airline's individual claims
    if(airlineData.includes(airline) == false){
      airlineData.name = airline;
      airlineData.claimTotal = claim;
    } else{
      continue;
    }

    // Build airline name array for dropdown box
    if (airlineNames.includes(airline) === false){
      airlineNames.push(airline);
    } else{
        continue;
    }

    // Build airport code array for dropdown box
    if (airportCodes.includes(airport) === false){
      airportCodes.push(airport);
    } else{
        continue;
    }

  }; // End loop through rows

我想要的结果如下:

 [{name:" ",claims:[]},
  {name:" ",claims:[]},
  {},
  //...
 ]

所以我可以根据通过下拉框选择的名称选择所需的数据集。

2 个答案:

答案 0 :(得分:1)

我收到了excel文件并且不确定这是否是你想要的,每月的平均值和总和是一个月份密钥的对象。

&#13;
&#13;
const dataPromise = fetch("https://raw.githubusercontent.com/amsterdamharu/amsterdamharu.github.io/master/data.txt")
.then(response=>response.text())
.then(
  text=>{
    const all = text.split("\n").map(row=>row.trim());
    const fields = all[0].split("\t").map(f=>f.trim());
    const info = all.slice(1).map(
      row=>row.split("\t").map(item=>item.trim())
    );
    return info.map(
      (row)=>
        row.reduce(
          (result,field,index)=>{
            result[fields[index]]=field;
            return result;
          },
          {}
        )
    );
  }
);
dataPromise.then(
  data=>{
    const raw =  data.reduce(
      (result,item)=>{
        const name = item["Airline Name"];
        const claims = (result[name] = (result[name] || {}));
        const date = new Date(item["Incident Date"]);
        const month = date.getMonth()+1;
        const year = date.getFullYear();
        const claim = Number(item["Close Amount"].replace(/[^0-9\.]/g,""));
        if(isNaN(claim)){
          debugger;
        }
        claims[`${month}-${year}`]=claims[`${month}-${year}`]||[];
        claims[`${month}-${year}`].push(claim);
        return result;
      },
      {}
    );
    console.log("raw object:",raw);
    const objectAveragePerMonth = Object.keys(raw).map(
      name=>({
          name,
          monthAverage:Object.entries(raw[name]).reduce(
            (result,[key,values])=>{
              const noZeros = values.filter(m=>m);//remove zero values
              result[key] = (noZeros.length)
                ? noZeros.reduce((sum,item)=>sum+item,0)/noZeros.length
                : 0;
              return result;
            },
            {}
          )
      })
    );
    console.log("average per month:",objectAveragePerMonth);
    const objectSumPerMonth = Object.keys(raw).map(
      name=>({
        name,
        totalPerMonth:Object.entries(raw[name]).reduce(
          (result,[key,values])=>{
            result[key] = values.reduce((sum,item)=>sum+item,0);
            return result;
          },
          {}
        )
      })
    );
    console.log("sum per month",objectSumPerMonth);
    const objectTotal = Object.keys(raw).map(
      name=>({
        name,
        total:Object.entries(raw[name]).reduce(
          (result,[key,values])=>result+values.reduce((sum,item)=>sum+item,0),
          0
        )
      })
    );
    console.log("total per airline",objectTotal);
  }
);
&#13;
&#13;
&#13;

您可以查看Array.prototype.reduceArray.prototype.mapObject.keys以更多地了解代码。

如果您需要任何帮助,请告诉我

答案 1 :(得分:0)

尝试以下方法:

&#13;
&#13;
// Code goes here

var jsonData = [{
  "Airline Name":"American Airlines ",
  "Airport Code":"ORD",
  "Airport Name":"Chicago O'Hare International Airport",
  "Claim Number":2010030168888,
  "Claim Site":"Checked Baggage",
  "Claim Type":"Passenger Property Loss",
  "Close Amount":0,
  "Date Received":40231,
  "Disposition":"Deny",
  "Incident Date":40182.25,
  "Item Category":"Personal Electronics; Travel Accessories"
}];
var map = {};
jsonData.forEach(function(obj){
  if(!map[obj["Airline Name"]])
    map[obj["Airline Name"]] = {};
  map[obj["Airline Name"]].name = obj["Airline Name"];
  map[obj["Airline Name"]].claims = map[obj["Airline Name"]].claims || [];
  map[obj["Airline Name"]].claims.push(obj["Claim Number"]);
  map[obj["Airline Name"]].claimTotal = map[obj["Airline Name"]].claimTotal !== undefined ? map[obj["Airline Name"]].claimTotal + obj["Close Amount"] : 0; 
});
var arr = Object.values(map);
console.log(arr);
&#13;
&#13;
&#13;