简体   繁体   English

如何将值插入到嵌套数组中

[英]How to insert values to a nested array

I am having one report array of objects which is used to show report data on the dashboard as below,我有一个对象的报告数组,用于在仪表板上显示报告数据,如下所示,

    report = [
              {id: "id01", code: "C001", name: "apples", type: "fruits", intime:[], quantity:[]},
              {id: "id01", code: "C001", name: "grapes", type: "fruits", intime:[], quantity:[]},
              {id: "id01", code: "C002", name: "apples", type: "fruits", intime:[], quantity:[]},
              {id: "id01", code: "C002", name: "grapes", type: "fruits", intime:[], quantity:[]}
             ]

And below array as input,在数组下方作为输入,

record = [
    {code: "C001",
    records :[{intime: "15:32:12", apples: "30", grapes: "80"},
            {intime: "15:32:13", apples: "34", grapes: "50"}]
    },  
    {code: "C002",
    records :[{intime: "15:32:12", apples: "56", grapes: "100"},
            {intime: "15:32:13", apples: "75", grapes: "38"}]
    },      
]

I want to put these values to arrays of report array as per matching condition so that report array should look like,我想根据匹配条件将这些值放入报告数组的数组中,以便报告数组看起来像,

report = [
  {id: "id01", code: "C001", name: "apples", type: "fruits", intime:["15:32:12","15:32:13"], quantity:[30,34]},
  {id: "id01", code: "C001", name: "grapes", type: "fruits", intime:["15:32:12","15:32:13"], quantity:[80,50]},
  {id: "id01", code: "C002", name: "apples", type: "fruits", intime:["15:32:12","15:32:13"], quantity:[56,75]},
  {id: "id01", code: "C002", name: "grapes", type: "fruits", intime:["15:32:12","15:32:13"], quantity:[100,38]}
]

I think , this can be achieved through array filter method in loop but that method does not seems efficient when record array is big in size.我认为,这可以通过循环中的数组过滤方法来实现,但是当记录数组的大小很大时,该方法似乎效率不高。 Please suggest me some better and efficient way to put values in such way.请建议我一些更好、更有效的方法来以这种方式放置值。

const finalReport = report.map(rep => {
  let obj = { ...rep };

  const recordFoundByCode = record.find(rec => rec.code === obj.code);

  if (recordFoundByCode) {
    recordFoundByCode.records.forEach(item => {
      obj.intime.push(item.intime);
      obj.quantity.push(item[obj.name]);
    });
  }

  return obj;
});

console.log(finalReport) // <== your desired array

Create an object that uses code as the keys to find all the related report objects.创建一个对象,使用code作为关键字来查找所有相关的report对象。 Then use nested loops to insert the times in all the intime arrays.然后使用嵌套循环在所有intime数组中插入时间。

 var report = [ {id: "id01", code: "C001", name: "apples", type: "fruits", intime:[], quantity:[]}, {id: "id01", code: "C001", name: "grapes", type: "fruits", intime:[], quantity:[]}, {id: "id01", code: "C002", name: "apples", type: "fruits", intime:[], quantity:[]}, {id: "id01", code: "C002", name: "grapes", type: "fruits", intime:[], quantity:[]} ]; record = [ {code: "C001", records :[{intime: "15:32:12", apples: "30", grapes: "80"}, {intime: "15:32:13", apples: "34", grapes: "50"}] }, {code: "C002", records :[{intime: "15:32:12", apples: "56", grapes: "100"}, {intime: "15:32:13", apples: "75", grapes: "38"}] }, ]; var report_by_code = report.reduce((o, r) => { if (o[r.code]) { o[r.code].push(r); } else { o[r.code] = [r]; } return o; }, {}); record.forEach(({code, records}) => records.forEach(({intime}) => report_by_code[code].forEach(r => r.intime.push(intime)))); console.log(report);

_.each(report, rep => {
    let code = rep.code;
    let name = rep.name;
    _.each(record, rec => {
        if(rec.code === code) {
            let recs = rec.records;
            _.each(recs, re => {
                rep.intime.push(re.intime);
                rep.quantity.push(re[name]);
            });
        }
    });
});

You could utilize the 'underscore' module for JavaScript in this scenario.在这种情况下,您可以使用 JavaScript 的“下划线”模块。 First I start with an each loop that iterates over each object in the report array.首先,我从迭代报告数组中的每个对象的 each 循环开始。 Then we can store the code and the name keys associated with that object.然后我们可以存储与该对象关联的代码和名称键。 We can then iterate over each record and if the codes are equal we can iterate over that particular object.然后我们可以迭代每个记录,如果代码相等,我们可以迭代那个特定的对象。 We can then iterate over each of the records and store the intime and then the fruit quantity based on the fruit name associated with the report object that we are currently iterating over.然后,我们可以迭代每条记录,并根据与我们当前正在迭代的报表对象关联的水果名称存储 intime 和水果数量。 We then push those values to the intime and quantity arrays associated with that report object.然后我们将这些值推送到与该报表对象关联的 intime 和数量数组。

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

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