Javascript 使用lodash函数确定对象中的一个元素是否失败
我有一个对象数组,看起来像Javascript 使用lodash函数确定对象中的一个元素是否失败,javascript,reactjs,lodash,Javascript,Reactjs,Lodash,我有一个对象数组,看起来像 const test = [{"id": "1", "status": "progress"},{"id": "1", "status": "book"},{"id": "1", "status": "completed"}, {&
const test = [{"id": "1", "status": "progress"},{"id": "1", "status": "book"},{"id": "1", "status": "completed"}, {"id": "1", "status": "done"}]
这里我想得到一个结果,如果给定数组中的任何对象的值不是“progress”
和“book”
,那么它应该返回false,否则应该返回true
我用了下面的方法
const issome = _.some(test, item => !_.includes(["progress", "book"], item.status))
如何实现这一点
谢谢。如果行得通,那就是正确的方法。你有
代码>在错误的位置。
您现在要做的是检查是否有某个项目的成功率不等于process或book。
如果没有必要,我建议不要使用lodash。
您可以使用JavaScript本机和
如果给定数组中的任何对象的值不是
“progress”和“book”应该返回false,否则应该返回false
真的
这个怎么样
_.every(test, item => item.status === "book" || item.status === "progress"))
const test=[{“id”:“1”,“status”:“book”},{“id”:“1”,“status”:“book”},{“id”:“1”,“status”:“book”},{“id”:“1”,“status”:“book”}]
console.log(124;.every(test,item=>item.status==“book”| | item.status==“progress”);
const test1=[{“id”:“1”,“status”:“xxxxx”},{“id”:“1”,“status”:“book”},{“id”:“1”,“status”:“book”},{“id”:“1”,“status”:“book”}]
log(124;.every(test1,item=>item.status==“book”| | item.status==“progress”)代码>
“这样做对吗?”对你有用吗?:)如果不是的话,问题是什么?这对我不起作用。。它总是真实地返回,它不起作用,我如何用洛兹方式编辑答案
const test = [{"id": "1", "status": "progress"},{"id": "1", "status": "book"},{"id": "1", "status": "completed"}, {"id": "1", "status": "done"}]
const issome = _.every(test, item => _.includes(["progress", "book"], item.status))
//issome===false because there are values with status other than "book" or "progress"
const test = [{"id": "1", "status": "progress"},{"id": "1", "status": "book"}]
const issome = _.every(test, item => _.includes(["progress", "book"], item.status))
//issome===true because there are values with status "book" or "progress" only
_.every(test, item => item.status === "book" || item.status === "progress"))