我有一个简单的对象数组。
只要数组中有一个具有特定属性值的对象,我如何生成while语句来执行某些操作?
let shirts = new Array();
let item1 = {
"color": "blue",
"size": "small"
}
shirts.push(item1);
while (shirts.some(e => e.color === "blue")) {
// do something
}
所以,当shirts
有一个颜色为蓝色、尺寸较小的项目时,请做点什么。
我找到了一些解决方案,但它们只检查一个值,我需要将其设为两个:
while (shirts.some(e => e.color === "blue")) {
// do something
}
这是一个while循环,所以我可以通过编程不断修改值,直到它最终不匹配为止。
最好定义您所引用的项,这将有助于调试和将来您执行代码
let this_is_true = shirts.some(e => e.color === 'blue' && e.size === 'small');
while (this_is_true){
// ..
//re-check for every iteration
this_is_true = shirts.some(e => e.color === 'blue' && e.size === 'small')
}
while (shirts.some(e => e.color === 'blue' && e.size === 'small')) {
// do something
}
或
while (shirts.filter(e => e.color === 'blue' && e.size === 'small').length > 0) {
// do something
}