Created
August 10, 2014 09:10
-
-
Save antixrist/faf300eb04c1aeca9137 to your computer and use it in GitHub Desktop.
VK API: Parse json Args to array.
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
/** | |
* For check: https://vk.com/dev/execute | |
*/ | |
// var myList = Args.list; | |
// var listSeparator = Args.listSeparator; | |
// var listItemsType = Args.listItemsType; | |
var myList = "123456,789012,345678,901234,567890"; | |
var listSeparator = ","; | |
var listItemsType = "integer"; | |
var result = []; | |
var charsLength = myList.length; | |
var currentPosition = 0; | |
var previousSeparatorPosition = -1; | |
var item; | |
while (currentPosition < charsLength) { | |
if (myList.substr(currentPosition, 1) == listSeparator) { | |
item = myList.substr(previousSeparatorPosition + 1, currentPosition - previousSeparatorPosition - 1); | |
if (listItemsType == "integer") { | |
item = parseInt(item); | |
} | |
result.push(item); | |
previousSeparatorPosition = currentPosition; | |
} | |
currentPosition = currentPosition + 1; | |
} | |
return result; |
а то последний элемент "проглатывается"
просто ставится запятая в конце списка, как завершение и все.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Надо добавить обработку ещё одного элемента после цикла while, а то последний элемент "проглатывается".