0

Here is a python code, I want to translate it to javascript, any help?

result = list()
...
if not string in result:
   result.append(string)

Thanks!

lito
  • 3,105
  • 11
  • 43
  • 71
  • 2
    Check out [arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array), [`.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) and [`.push`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push). – Mike Cluck Feb 10 '16 at 21:13
  • 2
    Also, in python, it reads slightly nicer if you do `if string not in result` :-) – mgilson Feb 10 '16 at 21:13
  • I don't know about javascript, but in Python you can ensure that only unique values are in your collection by using a set instead of a list. `result = set(strings)` is a lot easier than `for string in strings: if string not in result: result.append(string))` – Kevin Feb 10 '16 at 21:17
  • Jusnt In case... result = []; if(result.indexOf(string) > -1 ){ result.push(string); } – lito Feb 10 '16 at 21:20

0 Answers0