Timeline for Object prototyping
Current License: CC BY-SA 3.0
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 20, 2020 at 9:12 | history | edited | Community Bot |
Commonmark migration
|
|
| May 23, 2017 at 12:21 | history | edited | URL Rewriter Bot |
replaced http://stackoverflow.com/ with https://stackoverflow.com/
|
|
| Oct 31, 2013 at 15:53 | comment | added | A. Matías Quezada |
Easier functin in_array(target, array) { return array.map(String).indexOf(String(target).trim()) !== -1 } then in_array('1', [ 1, 2, 3, 'hi' ])
|
|
| Oct 31, 2013 at 12:28 | comment | added | HMR |
Quotes from op: if ((a == 'value1') || (a == 'value2') and I need to compare strings and numbers ('1' == 1 is right for my situation). ['1','2'].indexOf(1) return -1 As for the second part; I don't think so: var keys ={"1":true};console.log(keys["toString"]===true) (as used in reply to raina). I'm just saying that in my opinion raina's answer is more effective (would be mine but he beat me to it) and elegant to solve this particular problem.
|
|
| Oct 31, 2013 at 9:33 | comment | added | A. Matías Quezada |
Why whould you suppose OP's program need to parse un-trimmed strings? In any case, this can be easely added to the helper function functin in_array(target, array) { return array.indexOf(target.trim()) !== -1 }. By the way, if you use keys[value.trim()] if value is "constructor", "toString", "toValue" or any other property in Object.prototype it will return a false positive.
|
|
| Oct 31, 2013 at 2:01 | comment | added | HMR |
If you have to do it then the most elegant way would be to polyfil the indexOf. But in the OP's case [" 1 "].indexOf(1) would return -1 (false) where " 1 "== 1 would return true. So best solution in my opinion would be to use raina's answer and polyfil trim: keys={1:true};keys[" 1 ".trim()]
|
|
| Oct 30, 2013 at 22:25 | history | answered | A. Matías Quezada | CC BY-SA 3.0 |