所以我有一个数组,其中包含几个完全相同的编号条目,如下所示:
var levels = new Object;
levels = {
0:{
name: "xxx1",
ratesBR: {
stone: 1,
wood: 5,
copper: 0,
}
ratesFS: {
food: 10,
water: 10,
sacrifice: 0,
}
}
1:{
name:"xxx2",
ratesBR: {
stone: 1,
wood: 5,
copper: 0,
}
ratesFS: {
food: 10,
water: 10,
sacrifice: 0,
}
}
}
所以我的问题就是这样,说我想在所有“等级”(0,1等)中使用
检查'石头'是否大于或等于1if levels.XX.ratesBR.wood >= 1
但是代替XX,我想知道我可以用来表示我所看过的“全部”,而我最接近的是every()函数,但我认为这不是我想要的。我不知道“全部”能指甚至会被称为什么,所以我转向你们的方向进去。谢谢。
答案 0 :(得分:2)
所以我有一个阵列......
这不是一个数组,它是一个非数组对象。它只有属性名"0"
,"1"
等等(是的,它们是字符串,即使它们已被写成数字)。
我最接近的是every()函数,但我认为这不是我要找的。 p>
确实如此,但将它应用于非数组对象有点尴尬。我们可以使用Object.keys
获取对象的密钥,然后在其上使用every
。
在ES5中,你可以这样做:
if (Object.keys(levels).every(function(key) { return levels[key].ratesBR.wood >= 1; })) {
console.log("All are >= 1");
} else {
// No, at least one isn't
console.log("At least one isn't >= 1");
}
直播示例:
var levels = {
0: {
name: "xxx1",
//lifeStage:
ratesBR: {
stone: 1,
wood: 5,
copper: 0
},
ratesFS: {
food: 10,
water: 10,
sacrifice: 0
}
},
1: {
name: "xxx2",
//lifeStage:
ratesBR: {
stone: 2,
wood: 0,
copper: 0
},
ratesFS: {
food: 10,
water: 10,
sacrifice: 0
}
}
};
if (Object.keys(levels).every(function(key) { return levels[key].ratesBR.wood >= 1; })) {
console.log("All are >= 1");
} else {
// No, at least one isn't
console.log("At least one isn't >= 1");
}
在ES2015 +中,箭头功能可以使其更简洁:
if (Object.keys(levels).every(key => levels[key].ratesBR.wood >= 1)) {
console.log("All are >= 1");
} else {
// No, at least one isn't
console.log("At least one isn't >= 1");
}
直播示例:
var levels = {
0: {
name: "xxx1",
//lifeStage:
ratesBR: {
stone: 1,
wood: 5,
copper: 0
},
ratesFS: {
food: 10,
water: 10,
sacrifice: 0
}
},
1: {
name: "xxx2",
//lifeStage:
ratesBR: {
stone: 2,
wood: 0,
copper: 0
},
ratesFS: {
food: 10,
water: 10,
sacrifice: 0
}
}
};
if (Object.keys(levels).every(key => levels[key].ratesBR.wood >= 1)) {
console.log("All are >= 1");
} else {
// No, at least one isn't
console.log("At least one isn't >= 1");
}
如果levels
确实是一个数组(我建议使用),您只需直接使用every
:
// IF `levels` WERE AN ARRAY
if (levels.every(entry => entry.ratesBR.wood >= 1)) {
console.log("All are >= 1");
} else {
// No, at least one isn't
console.log("At least one isn't >= 1");
}
直播示例:
var levels = [
{
name: "xxx1",
//lifeStage:
ratesBR: {
stone: 1,
wood: 5,
copper: 0
},
ratesFS: {
food: 10,
water: 10,
sacrifice: 0
}
},
{
name: "xxx2",
//lifeStage:
ratesBR: {
stone: 2,
wood: 0,
copper: 0
},
ratesFS: {
food: 10,
water: 10,
sacrifice: 0
}
}
];
if (levels.every(entry => entry.ratesBR.wood >= 1)) {
console.log("All are >= 1");
} else {
// No, at least one isn't
console.log("At least one isn't >= 1");
}
ES5版本:
// IF `levels` WERE AN ARRAY
if (levels.every(function(entry) { return entry.ratesBR.wood >= 1; })) {
console.log("All are >= 1");
} else {
// No, at least one isn't
console.log("At least one isn't >= 1");
}
答案 1 :(得分:-1)
你可以这样做:
var levels = {
0:{
name: "xxx1",
//lifeStage:
ratesBR: {
stone: 1,
wood: 5,
copper: 0
},
ratesFS: {
food: 10,
water: 10,
sacrifice: 0
}
},
1:{
name: "xxx1",
//lifeStage:
ratesBR: {
stone: 1,
wood: 5,
copper: 0
},
ratesFS: {
food: 10,
water: 10,
sacrifice: 0
}
}
}
var array = $.map(levels, function(value, index) {
return [value];
});
for(var i =0; i < array.length; i++){
console.log(array[i].ratesBR.stone);
}