[英]how to use 'includes' correctly in vue
I want to show an error message if object in an array has the string FAILED
.如果数组中的 object 具有字符串FAILED
,我想显示一条错误消息。 My array looks like this:我的数组如下所示:
And my code to check if one object has state: "FAILED"
looks like this:我的代码检查一个 object 是否有state: "FAILED"
看起来像这样:
hasFailedTask() {
console.log(this.model.status.tasks);
return this.model.status.tasks.state.includes('FAILED');
},
but then I always get the following error message in my console:但是我总是在控制台中收到以下错误消息:
vue.runtime.esm.js:587 [Vue warn]: Error in render: "TypeError: Cannot read property 'includes' of undefined"
Does anyone know the problem and could support me?有谁知道这个问题并且可以支持我?
Thank you in advance!先感谢您!
You could use find
method since this.model.status.tasks
is an array:您可以使用find
方法,因为this.model.status.tasks
是一个数组:
hasFailedTask() {
return this.model.status.tasks.find(task=>task.state.includes('FAILED'));
},
your function should look like this:你的 function 应该是这样的:
hasFailedTask() {
//console.log(this.model.status.tasks);
return this.model.status.tasks.some(({state}) => state === "FAILED")
},
You can also use flatMap to allow an includes check:您还可以使用 flatMap 来允许包含检查:
hasFailedTask() {
return this.model.status.tasks.flatMap(t => t.state).includes('FAILED');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.