读取JSON文件并提取数据

时间:2019-03-30 08:24:30

标签: javascript geojson

乔布森档案

{
  "customer": "Haya",
  "oliveQuantity": 2,
  "oliveType": "Picual",
  "harvestDate": "12/8/2019",
  "sourceRegion": "Al Rayyan",
  "receptionDate": "9/9/2019",
  "millingDate": "20/11/2019",
  "jobId": 2222,
  "productCategory": "Container",
  "jobStatus": "Pending",
  "containerAmount": 1,
  "ContainerType": "BigContainer"

}

数据Jason文件

"container": [

  {
    "ContainerType": "BigContainer",
    "ContainerPrice":30

  },
  {
    "ContainerType": "mediumContainer",
    "ContainerPrice":25

  },
  {
    "ContainerType":  "smoilContainer",
    "ContainerPrice":20

  }
]

发票存储库类

async addInvoice(invoice) {

    try {

        const Job = await this.getJobs();
        if (Job.jobStatus == "Completed") {

        invoice.invoiceNo = uuid.v4();

        invoice.invoiceDate = Date.now();

        invoice.oliveQuantity = Job.oliveQuantity;


        if (Job.productCategory == "Container") {

           let datas = await this.getData();


            invoice.ContainerType =Job.ContainerType

            //datas.filter(data => data.container.find(c => c.includes(invoice.ContainerType );


            invoice.containerAmount = Job.containerAmount;
            invoice.containerType = Job.containerType;


      }
        invoice.push(invoice);

        return await this.saveInvoice(invoice);
    }

} catch (err) {
    console.log(err);
    }

}

在方法 addInvoice(invoice)中,我需要知道客户使用哪种类型的集装箱,然后提取价格。 然后取集装箱价格,乘以集装箱数量。 容器数量应从Job Jason文件中提取

1 个答案:

答案 0 :(得分:0)

如果Container数组中对象的顺序始终相同并且已知,则只需执行Container[1]

否则,您必须根据要选择Container数组中元素之一的条件来更具体

问题更新后

invoice.ContainerObjectFromData = datas.container.find(c => c.ContainerType === invoice.ContainerType); 假设可以直接在getData调用返回的datas对象中找到datas.container。