数组数据在3个不同的列中

huangapple go评论52阅读模式
英文:

Array data in 3 different column

问题

我有一个数组:

[
  {name: 'task1', status: 'new'},
  {name: 'task2', status: 'progress'},
  {name: 'task3', status: 'completed'},
  {name: 'task4', status: 'progress'},
  {name: 'task5', status: 'new'}
]

我需要在3列中显示数组的列表。

新任务 进行中任务 完成的任务
task1 task2 task3
task5 task4

对于这个问题,你可以创建3个不同的数组,然后进行迭代。是否有更好的方法,要看你的具体需求和代码框架而定。

英文:

I have an array:

[
  {name: 'task1', status: 'new'},
  {name: 'task2', status: 'progress'},
  {name: 'task3', status: 'completed'},
  {name: 'task4', status: 'progress'},
  {name: 'task5', status: 'new'}
]

I need to show the list of the array in 3 columns.

New task progressTask completed task
task1 task2 task3
task5 task5

what will be the best process for this.

I can create 3 different array and then iterate it. is there any better way than this.

答案1

得分: 1

我已经创建了一个小的JavaScript代码片段,演示了如何实现这个逻辑。这只是一个示例,你需要根据React的要求进行修改。

const tasks = [{
    name: 'task1',
    status: 'new'
  },
  {
    name: 'task2',
    status: 'progress'
  },
  {
    name: 'task3',
    status: 'completed'
  },
  {
    name: 'task4',
    status: 'progress'
  },
  {
    name: 'task5',
    status: 'new'
  }
];

const newTasks = [];
const inProgressTasks = [];
const completedTasks = [];

for (let task of tasks) {
  switch (task.status) {
    case "new":
      newTasks.push(task);
      break;
    case "progress":
      inProgressTasks.push(task);
      break;
    case "completed":
      completedTasks.push(task);
      break;
  }
}

const numTableRows = Math.max(newTasks.length, inProgressTasks.length, completedTasks.length);

const rows = [];

for (let n = 0; n < numTableRows; n++) {
  cellNewTask = newTasks[n]?.name ?? '-';
  cellInProgressTask = inProgressTasks[n]?.name ?? '-';
  cellCompletedTask = completedTasks[n]?.name ?? '-';

  rows.push([cellNewTask, cellInProgressTask, cellCompletedTask]);
}

console.log(rows);

你现在只需在React中循环遍历这些行并渲染表格即可。

英文:

I've created a small snippet in JS on how you could do it. This is just an example of how the logic could look like. Of course, you have to modify it to make it work for React.

But you get the idea:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const tasks = [{
    name: &#39;task1&#39;,
    status: &#39;new&#39;
  },
  {
    name: &#39;task2&#39;,
    status: &#39;progress&#39;
  },
  {
    name: &#39;task3&#39;,
    status: &#39;completed&#39;
  },
  {
    name: &#39;task4&#39;,
    status: &#39;progress&#39;
  },
  {
    name: &#39;task5&#39;,
    status: &#39;new&#39;
  }
];

const newTasks = [];
const inProgressTasks = [];
const completedTasks = [];

for (let task of tasks)&#160; {
  switch (task.status) {
    case &quot;new&quot;:
      newTasks.push(task);
      break;
    case &quot;progress&quot;:
      inProgressTasks.push(task);
      break;
    case &quot;completed&quot;:
      completedTasks.push(task);
      break;
  }
}

const numTableRows = Math.max(newTasks.length, inProgressTasks.length, completedTasks.length);

const rows = [];

for (let n = 0; n &lt; numTableRows; n++) {
  cellNewTask = newTasks[n]?.name ?? &#39;-&#39;;
  cellInProgressTask = inProgressTasks[n]?.name ?? &#39;-&#39;;
  cellCompletedTask = completedTasks[n]?.name ?? &#39;-&#39;;

  rows.push([cellNewTask, cellInProgressTask, cellCompletedTask]);
}

console.log(rows);

// Now you just need to loop over the rows with React and render the Table

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月21日 18:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定