-
-
Save petevb/c30184311ee3e5a66747 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/lajuri
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
console.clear(); | |
// .count, not length | |
var a = { | |
0: { Id: "abc", Price: 10 }, | |
1: { Id: "def", Price: 20 }, | |
count: 2 | |
}; | |
// ONLY .keys works when length is NOT specified. | |
var a1 = Array.from(a, v => v); | |
console.log ("a1", a1); | |
var a2 = Object.keys(a).filter(k => k !== "count").map(k => a[k]); | |
console.log ("a2", a2); | |
var a3 = Array.prototype.slice.call(a, 0); | |
console.log ("a3", a3); | |
// b has a .length property. | |
var b = { | |
0: { Id: "abc", Price: 10 }, | |
1: { Id: "def", Price: 20 }, | |
length: 2 | |
}; | |
/* | |
// All these methods work when length is specified. | |
var b1 = Array.from(b, v => v); | |
console.log ("b1", b1); | |
var b2 = Object.keys(b).map(k => b[k]); | |
console.log ("b2", b2); | |
var b3 = Array.prototype.slice.call(b, 0); | |
console.log ("b3", b3); | |
*/ | |
// what about hashes for lookup? | |
console.log ("**** hashtable ****"); | |
var a4 = a2.reduce((prev, cur) => { | |
prev[cur.Id] = cur.Price; | |
return prev; | |
}, {}); | |
console.log ("a4", a4); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">console.clear(); | |
// .count, not length | |
var a = { | |
0: { Id: "abc", Price: 10 }, | |
1: { Id: "def", Price: 20 }, | |
count: 2 | |
}; | |
// ONLY .keys works when length is NOT specified. | |
var a1 = Array.from(a, v => v); | |
console.log ("a1", a1); | |
var a2 = Object.keys(a).filter(k => k !== "count").map(k => a[k]); | |
console.log ("a2", a2); | |
var a3 = Array.prototype.slice.call(a, 0); | |
console.log ("a3", a3); | |
// b has a .length property. | |
var b = { | |
0: { Id: "abc", Price: 10 }, | |
1: { Id: "def", Price: 20 }, | |
length: 2 | |
}; | |
/* | |
// All these methods work when length is specified. | |
var b1 = Array.from(b, v => v); | |
console.log ("b1", b1); | |
var b2 = Object.keys(b).map(k => b[k]); | |
console.log ("b2", b2); | |
var b3 = Array.prototype.slice.call(b, 0); | |
console.log ("b3", b3); | |
*/ | |
// what about hashes for lookup? | |
console.log ("**** hashtable ****"); | |
var a4 = a2.reduce((prev, cur) => { | |
prev[cur.Id] = cur.Price; | |
return prev; | |
}, {}); | |
console.log ("a4", a4);</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.clear(); | |
// .count, not length | |
var a = { | |
0: { Id: "abc", Price: 10 }, | |
1: { Id: "def", Price: 20 }, | |
count: 2 | |
}; | |
// ONLY .keys works when length is NOT specified. | |
var a1 = Array.from(a, v => v); | |
console.log ("a1", a1); | |
var a2 = Object.keys(a).filter(k => k !== "count").map(k => a[k]); | |
console.log ("a2", a2); | |
var a3 = Array.prototype.slice.call(a, 0); | |
console.log ("a3", a3); | |
// b has a .length property. | |
var b = { | |
0: { Id: "abc", Price: 10 }, | |
1: { Id: "def", Price: 20 }, | |
length: 2 | |
}; | |
/* | |
// All these methods work when length is specified. | |
var b1 = Array.from(b, v => v); | |
console.log ("b1", b1); | |
var b2 = Object.keys(b).map(k => b[k]); | |
console.log ("b2", b2); | |
var b3 = Array.prototype.slice.call(b, 0); | |
console.log ("b3", b3); | |
*/ | |
// what about hashes for lookup? | |
console.log ("**** hashtable ****"); | |
var a4 = a2.reduce((prev, cur) => { | |
prev[cur.Id] = cur.Price; | |
return prev; | |
}, {}); | |
console.log ("a4", a4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment