admin管理员组文章数量:1023738
I have a sorted array, which I add elements to as the server gives them to me. The trouble I'm having is determining where to place my new element and then placing it in the same loop
in javascript this would look like this
for(var i = 0; i < array.length; ++i){
if( element_to_add < array[i]){
array.splice(i,0,element_to_add);
break;
}
}
The problem is that in coffee script I dont have access to the counter, so I cant tell it to splice my array at the desired index.
How can I add an element to a sorted array in CoffeeScript?
I have a sorted array, which I add elements to as the server gives them to me. The trouble I'm having is determining where to place my new element and then placing it in the same loop
in javascript this would look like this
for(var i = 0; i < array.length; ++i){
if( element_to_add < array[i]){
array.splice(i,0,element_to_add);
break;
}
}
The problem is that in coffee script I dont have access to the counter, so I cant tell it to splice my array at the desired index.
How can I add an element to a sorted array in CoffeeScript?
Share Improve this question edited Jun 5, 2013 at 20:40 Loourr asked Jun 5, 2013 at 1:25 LoourrLoourr 5,12510 gold badges45 silver badges69 bronze badges 3- How are you iterating over the array now? – Blender Commented Jun 5, 2013 at 1:27
-
for element in array
and I also tried rapping some plane javascript code in back-ticks, but it keeps on puttingreturn
infront once it's piled – Loourr Commented Jun 5, 2013 at 1:32 - Assuming your array is not sorted in reverse, that code is wrong - it inserts the element just in the first position. And it forgets to break after having it inserted, so it will loop forever. – Bergi Commented Jun 5, 2013 at 1:44
3 Answers
Reset to default 5The default for
loop returns the index as well:
a = [1, 2, 3]
item = 2
for elem, index in a
if elem >= item
a.splice index, 0, item
break
You may want to do a binary search instead.
If you are using Underscore.js (very remended for these kind of array manipulations), _.sortedIndex
, which returns the index at which a value should be inserted into an array to keep it ordered, can e very handy:
sortedInsert = (arr, val) ->
arr.splice (_.sortedIndex arr, val), 0, val
arr
If you're not using Underscore, making your own sortedIndex
is not that hard either; is's basically a binary search (if you want to keep its plexity as O(log n)):
sortedIndex = (arr, val) ->
low = 0
high = arr.length
while low < high
mid = Math.floor (low + high) / 2
if arr[mid] < val then low = mid + 1 else high = mid
low
If I understood you correctly, why not save the position, like so,
var pos=-1;
for(var i = 0; i < array.length; ++i){
if( element_to_add < array[i]){
pos=i; break;
}
}
if(pos<0)
array.push(element_to_add);
else array.splice(pos,0,element_to_add);
I have a sorted array, which I add elements to as the server gives them to me. The trouble I'm having is determining where to place my new element and then placing it in the same loop
in javascript this would look like this
for(var i = 0; i < array.length; ++i){
if( element_to_add < array[i]){
array.splice(i,0,element_to_add);
break;
}
}
The problem is that in coffee script I dont have access to the counter, so I cant tell it to splice my array at the desired index.
How can I add an element to a sorted array in CoffeeScript?
I have a sorted array, which I add elements to as the server gives them to me. The trouble I'm having is determining where to place my new element and then placing it in the same loop
in javascript this would look like this
for(var i = 0; i < array.length; ++i){
if( element_to_add < array[i]){
array.splice(i,0,element_to_add);
break;
}
}
The problem is that in coffee script I dont have access to the counter, so I cant tell it to splice my array at the desired index.
How can I add an element to a sorted array in CoffeeScript?
Share Improve this question edited Jun 5, 2013 at 20:40 Loourr asked Jun 5, 2013 at 1:25 LoourrLoourr 5,12510 gold badges45 silver badges69 bronze badges 3- How are you iterating over the array now? – Blender Commented Jun 5, 2013 at 1:27
-
for element in array
and I also tried rapping some plane javascript code in back-ticks, but it keeps on puttingreturn
infront once it's piled – Loourr Commented Jun 5, 2013 at 1:32 - Assuming your array is not sorted in reverse, that code is wrong - it inserts the element just in the first position. And it forgets to break after having it inserted, so it will loop forever. – Bergi Commented Jun 5, 2013 at 1:44
3 Answers
Reset to default 5The default for
loop returns the index as well:
a = [1, 2, 3]
item = 2
for elem, index in a
if elem >= item
a.splice index, 0, item
break
You may want to do a binary search instead.
If you are using Underscore.js (very remended for these kind of array manipulations), _.sortedIndex
, which returns the index at which a value should be inserted into an array to keep it ordered, can e very handy:
sortedInsert = (arr, val) ->
arr.splice (_.sortedIndex arr, val), 0, val
arr
If you're not using Underscore, making your own sortedIndex
is not that hard either; is's basically a binary search (if you want to keep its plexity as O(log n)):
sortedIndex = (arr, val) ->
low = 0
high = arr.length
while low < high
mid = Math.floor (low + high) / 2
if arr[mid] < val then low = mid + 1 else high = mid
low
If I understood you correctly, why not save the position, like so,
var pos=-1;
for(var i = 0; i < array.length; ++i){
if( element_to_add < array[i]){
pos=i; break;
}
}
if(pos<0)
array.push(element_to_add);
else array.splice(pos,0,element_to_add);
本文标签: javascriptadd element to ordered array in CoffeeScriptStack Overflow
版权声明:本文标题:javascript - add element to ordered array in CoffeeScript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745533816a2154870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论