如何将对象数组转换为数组

时间:2018-03-05 17:16:03

标签: javascript

我有一个对象数组:

jobs: {
      tasks:
        {
          Clean:[
            {time: "08:00", task: 'floor 1'},
            {time: "09:00", task: 'floor 2'}
          ],

          Print:[
            {time: "10:00", task: 'x files'},
            {time: "11:00", task: 'x files 2.0'}
          ]                      

        }
  }

我需要将数据转换为类似的内容

 this.listTimeJob.Clean = ['','','','','','','', '','floor 1','floor 2','','','','','','','','','','','','','','',''];
 this.listTimeJob.Print = ['','','','','','','','','','','x files','x files 2.0','','','','','','','','','','','','',''];

所以我可以循环并获得结果 enter image description here

我真的被这个困住了,我仍然想弄明白......如果有人能给我一点帮助我真的很感激..

3 个答案:

答案 0 :(得分:1)

您可以使用Array.prototype.reduce,将累加器传递为Array(24).fill(""),并且每次迭代都会根据时间值更改累加器的索引值。

var cleanData = data.jobs.tasks.Clean.reduce(
  (acc, el, i) => {
    var index = +el.time.split(":")[0];
    acc[index] = el.task;
    return acc;
  }, Array(24).fill("")
);



var data = {
  jobs: {
    tasks: {
      Clean: [{
          time: "08:00",
          task: 'floor 1'
        },
        {
          time: "09:00",
          task: 'floor 2'
        }
      ],

      Print: [{
          time: "10:00",
          task: 'x files'
        },
        {
          time: "11:00",
          task: 'x files 2.0'
        }
      ]

    }
  }
}

var cleanData = data.jobs.tasks.Clean.reduce(
  (acc, el, i) => {
    var index = +el.time.split(":")[0];
    acc[index] = el.task;
    return acc;
  }, Array(24).fill("")
);

var printData = data.jobs.tasks.Print.reduce(
  (acc, el, i) => {
    var index = +el.time.split(":")[0];
    acc[index] = el.task;
    return acc;
  }, Array(24).fill("")
);

console.log(cleanData);
console.log(printData)




答案 1 :(得分:0)

我会创建两个包含所有空数据的数组,然后遍历您的任务并获得用作索引的时间:

this.listTimeJob.Clean = [];
this.listTimeJob.Print = [];

for (var x = 0; x < 25; x++) {
    this.listTimeJob.Clean.push("");
    this.listTimeJob.Print.push("");
}

for (var y = 0; y < jobs.tasks.Clean.length; y++){
    var index = parseInt(jobs.tasks.Clean[y].time.split(":"), 10);
    this.listTimeJob.Clean[index] = jobs.tasks.Clean[y].task
}

for (var y = 0; y < jobs.tasks.Print.length; y++){
    var index = parseInt(jobs.tasks.Print[y].time.split(":"), 10);
    this.listTimeJob.Print[index] = jobs.tasks.Print[y].task
}

答案 2 :(得分:0)

我的普通JS版

&#13;
&#13;
var jobs = {
      tasks:
        {
          Clean:[
            {time: "08:00", task: 'floor 1'},
            {time: "09:00", task: 'floor 2'}
          ],

          Print:[
            {time: "10:00", task: 'x files'},
            {time: "11:00", task: 'x files 2.0'}
          ]                      

        }
  }
 
function pad(num) {
  return ("0"+num).slice(-2);
} 
var th = document.querySelector("#th"),
    tb = document.querySelector("#tb"),
    time=[], 
    clean = new Array(24), 
    print = new Array(24), 
    tasks=jobs.tasks;

for (var i=0;i<24;i++) { // from 00:00 to 23:00
  time.push(""+pad(i)+":00")
}  
for (var j=0;j<tasks.Clean.length;j++) {
  clean[parseInt(tasks.Clean[j].time)]=tasks.Clean[j].task;
}
for (var j=0;j<tasks.Print.length;j++) {
  print[parseInt(tasks.Print[j].time)]=tasks.Print[j].task;
}

th.innerHTML="<tr><th>"+time.join("</th><th>")+"</th></tr>";
tb.innerHTML="<tr><td>"+clean.join("</td><td>")+"</td></tr>";
tb.innerHTML+="<tr><td>"+print.join("</td><td>")+"</td></tr>";
&#13;
td { border:1px solid black }
&#13;
<table>
<thead id="th"></thead>
<tbody id="tb"></tbody>
</table>
&#13;
&#13;
&#13;