Skip to content

Instantly share code, notes, and snippets.

@asserchiu
Last active August 29, 2015 13:58
Show Gist options
  • Save asserchiu/10273352 to your computer and use it in GitHub Desktop.
Save asserchiu/10273352 to your computer and use it in GitHub Desktop.
JavaScript `for ... in ... xxx.hasOwnProperty`
var a = {
'0': 3,
'3': 5,
'10': 8
};
function fn(dics, num) {
var resp = null;
var keys = Object.keys(dics);
console.log("----------");
console.log("num:", num);
// console.log("dics:", dics);
// console.log("keys:", keys);
for (var key in keys) { // `key` is a numeric index of keys here. [0,1,2,...]
// console.log("key (index):", key);
if (dics.hasOwnProperty(keys[key])) {
if (num >= Number(keys[key])) {
resp = dics[Number(keys[key])];
// console.log(num, ">=", Number(keys[key]));
console.log("resp:", resp);
};
};
};
// console.log(resp);
return resp;
}
var i = 12;
while (i-- > 0) {
console.log(fn(a, i));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment