简体   繁体   English

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

[英]Creating array of objects from large json data set

I am building my first larger application with AngularJS and am having difficulty building an array of objects for graphing data. 我正在使用AngularJS构建我的第一个更大的应用程序,并且难以构建用于图形化数据的对象数组。 Currently, I am taking an excel file and creating JSON data with SheetJS. 目前,我正在获取一个Excel文件,并使用SheetJS创建JSON数据。 There are on the order of 40,000 entries stored in a variable "jsonData" in the format: 存储在变量“ jsonData”中的顺序为40,000,格式为:

控制台的JSON打印输出

My end goal is to graph the "Close Amount" per month for every airline. 我的最终目标是绘制每个航空公司每月的“关闭金额”图。 My idea is the creation of an array of objects that will store the airline name, and an array of claims data (called airlineData). 我的想法是创建一个将存储航空公司名称的对象数组和一个索赔数据数组(称为airlineData)。 I am having difficulty with looping through jsonData to add the desired info into the array of objects based on an if statement to see if that airline name has been added already. 我很难遍历jsonData以根据if语句将所需的信息添加到对象数组中,以查看是否已经添加了航空公司名称。

I have the code on github- https://github.com/mikebly/tsa-luggage-analysis 我在github- https://github.com/mikebly/tsa-luggage-analysis上有代码

For simplicity, here is my entire loop through jsonData: 为了简单起见,这是我遍历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

My desired result would look something like: 我想要的结果看起来像:

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

so I can select the desired dataset based on the name selected through a dropdown box. 因此我可以根据通过下拉框选择的名称选择所需的数据集。

I got the excel file and not sure if this is what you would like, average and sum per month is an object with keys of month-year. 我得到了excel文件,并且不确定这是否是您想要的,每月的平均值和总和是具有月-年键的对象。

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

You could look at Array.prototype.reduce , Array.prototype.map and Object.keys to understand the code more. 您可以查看Array.prototype.reduceArray.prototype.mapObject.keys来了解更多代码。

Let me know if you need any help 让我知道您是否需要任何帮助

Try the following: 请尝试以下操作:

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

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

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