试图循环遍历元素为对象的数组

时间:2015-08-08 09:02:14

标签: javascript arrays loops

我想知道为什么代码输出错误,你可以在对象中看到星期三= 34.我想我可能有更改i变量的问题。

var lookup = [{"Monday" : 12}, {"Wednesday" : 34},{"Thursday" : 0},{"Saturday" : 56} ]
// console.log(lookup[1]["Wednesday"] == 34) // prints out true
function ami(day, num){
    var a;
    for(var i = 0; i < lookup.length; i++){
        if(lookup[i][day] == num || 
            day == "Tuesday" && num >95 ||
             day == "Friday" && num %2 == 0 || 
             day == "Sunday" && num == 666 ||
             day == "Sunday" && num == -666){
            a = true
        }else{
            a = false
        }

     }
     return a;

}
console.log(ami("Wednesday", 34))

3 个答案:

答案 0 :(得分:0)

a循环的每次迭代中,您正在重写for的值。实质上,您正在做的是检查lookup数组的最后一项,因为以前的结果总是被覆盖。

我不确切地知道你在条件下想要达到的目标,但这可能就是你所需要的:

function ami(day, num){
    for(var i = 0; i < lookup.length; i++){
        if(lookup[i][day] == num || 
            day == "Tuesday" && num >95 ||
            day == "Friday" && num %2 == 0 || 
            day == "Sunday" && num == 666 ||
            day == "Sunday" && num == -666){
           return true;
        }
    }
    return false;
}

答案 1 :(得分:0)

当你的代码达到i = 1时,a确实设置为true。但之后允许循环继续,所以当我增加到2时,a再次设置为false。

您可以在确定返回值应为true后立即从函数返回来解决此问题:

var lookup = [{"Monday" : 12}, {"Wednesday" : 34},{"Thursday" : 0},{"Saturday" : 56} ]
function ami(day, num){
    for(var i = 0; i < lookup.length; i++){
        if(lookup[i][day] == num || 
            day == "Tuesday" && num >95 ||
             day == "Friday" && num %2 == 0 || 
             day == "Sunday" && num == 666 ||
             day == "Sunday" && num == -666){
            return true;
        }
     }
}
console.log(ami("Wednesday", 34));

如果使用普通对象作为查找表,则可以进一步简化代码:

var lookup = {"Monday" : 12, "Wednesday" : 34, "Thursday" : 0, "Saturday" : 56};
function ami(day, num){
    return (lookup[day] == num || 
        day == "Tuesday" && num >95 ||
         day == "Friday" && num %2 == 0 || 
         day == "Sunday" && num == 666 ||
         day == "Sunday" && num == -666);
}
console.log(ami("Wednesday", 34));

答案 2 :(得分:0)

Array.some()应该这样做。

var lookup = [{ "Monday": 12 }, { "Wednesday": 34 }, { "Thursday": 0 }, { "Saturday": 56 }];
function ami(day, num) {
    return lookup.some(function (a) { return a[day] === num; }) ||
        day == "Tuesday" && num > 95 ||
        day == "Friday" && num % 2 == 0 ||
        day == "Sunday" && num == 666 ||
        day == "Sunday" && num == -666
}
document.write(ami("Wednesday", 34));

另一种可能性是将查找数组优化为具有多个属性的对象,例如

var lookup = { "Monday": 12, "Wednesday": 34, "Thursday": 0, "Saturday": 56 };

另一个版本就是这个。我接受了你的代码并改变了条件和循环条件的行为。因此,首先评估给定条件,然后在必要时对数组进行迭代。取变量a以及停止迭代的指标以及返回值。

var lookup = [{ "Monday": 12 }, { "Wednesday": 34 }, { "Thursday": 0 }, { "Saturday": 56 }];
function ami(day, num) {
    var a = false;
    if (day == "Tuesday" && num > 95 ||
        day == "Friday" && num % 2 == 0 ||
        day == "Sunday" && num == 666 ||
        day == "Sunday" && num == -666) {
        a = true;
    }
    for (var i = 0; !a && i < lookup.length; i++) {
        a = lookup[i][day] == num;
    }
    return a;
}
document.write(ami("Wednesday", 34));