简体   繁体   English

需要在 React.js 中检查 object 数组

[英]Need to check array of object in React.js

I have a bunch of objects inside an array and I have to check status of each object and return a single value for each array.我在一个数组中有一堆对象,我必须检查每个 object 的状态并为每个数组返回一个值。

(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {projId: 1, task: 'task 1', status: 'completed'}
1: {projId: 1, task: 'task 2', status: 'completed'}
2: {projId: 1, task: 'task 3', status: 'completed'}
3: {projId: 1, task: 'task 4', status: 'completed'}
4: {projId: 1, task: 'task 5', status: 'completed'}
5: {projId: 1, task: 'task 6', status: 'completed'}
6: {projId: 1, task: 'task 7', status: 'completed'}
7: {projId: 1, task: 'task 8', status: 'completed'}
8: {projId: 1, task: 'task 9', status: 'completed'}
9: {projId: 1, task: 'task 10', status: 'completed'}

Need to check the status, and if it is completed for the whole array then return competed else return in progress .需要检查状态,如果整个数组完成则返回competed否则返回in progress The code I tried is given below, but that returns for each object in array.下面给出了我尝试的代码,但它返回数组中的每个 object。

{GroupedData[key].map(status => ((status.status === "completed")? "Completed" : "In Progress"))} 

And I am writing this inside the return of export function in React.js我正在 React.js 中的 export function 的返回中写这个

.map() method is used to change values in a given array and returns that new modified array. .map()方法用于更改给定数组中的值并返回新修改的数组。 In your case, your code changes each value in your array to completed / in progress depending on the status of the object.在您的情况下,您的代码根据 object 的状态将数组中的每个值更改为completed / in progress中。

In your case, I think that using .some() or .every() will be the best way to go about this.在你的情况下,我认为使用.some().every()将是 go 的最佳方式。 In the code below you are checking that every project has status completed and if that returns true it assigns 'completed' to result , if not it assigns 'in progress'在下面的代码中,您正在检查每个项目的状态是否已completed ,如果返回 true,则将'completed'分配给result ,否则将分配'in progress'

const result = data.every((project) => project.status === 'completed') ? 'completed' : 'in progress';

try this:尝试这个:

yourArrayObject.every((itemObject) => itemObject.status === 'completed') ? 'Completed' : 'In Progress';

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

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