Group an array with same value in actionscript - arrays

I have an array:
var exArr:Array = [5,6,10,6,5,11,7,9,12,8,8,13,7,9,14];
I want to array:
var resultArr:Array = [5,6,7,8,9,10,11,12,13,14];

This may use full to you.
var a:Array = [5,6,10,6,5,11,7,9,12,8,8,13,7,9,14];
a.sort();
var i:int = 0;
while(i < a.length) {
while(i < a.length+1 && a[i] == a[i+1]) {
a.splice(i, 1);
}
i++;
}
for other, see here

Try this:
var exArr:Array = [5,6,10,6,5,11,7,9,12,8,8,13,7,9,14];
function group(subject:Array):Array
{
var base:Array = subject.slice().sort(Array.NUMERIC);
var prev:Number = base[0];
for(var i:int = 1; i < base.length; i++)
{
if(base[i] === prev)
{
base.splice(i, 1);
i--;
}
prev = base[i];
}
return base;
}
trace( group(exArr) );

Related

Is there any way to replace text in ChromiumBrowser like Chrome already has?

I'm making a web editor application using CefSharp WinForms library, but I couldn't find a way to replace text from CefSharp API.
There is a find method in WebBrowserExtensions but no replace method.
Question 1:
Does anyone know where replacing text method is in CefSharp?
Or there is no way to replace text in CefSharp? If yes, I need to find a detour for it.
Question 2:
There are yellow blocks marked found words when I try Find method, but those blocks are not a part of selection range of window object in HTML. Are those blocks made by native not web browser?
Answer:
I made "find and replace" function by myself using javascript, so if someone tries to do something like me then you can use below codes:
var lastsearchedNodeIndex = -1;
var lastSearchedTextIndex = -1;
var lastRange = null;
function getTextLengthFromStartTo(targetNodeIndex) {
var childNodes = editor.childNodes;
var textLength = 0;
if (targetNodeIndex >= childNodes.length) {
return editor.textContent.length;
}
for (var i = 0; i < targetNodeIndex; i++) {
if (childNodes[i].textContent != null) {
textLength += childNodes[i].textContent.length;
}
}
return textLength;
}
function getCurrentCaretIndex() {
var currentCaretIndex = 0;
var doc = editor.ownerDocument || editor.document;
var win = doc.defaultView || doc.parentWindow;
var sel;
if (typeof win.getSelection != "undefined") {
sel = win.getSelection();
if (sel.rangeCount > 0) {
var range = win.getSelection().getRangeAt(0);
var preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(editor);
preCaretRange.setEnd(range.endContainer, range.endOffset);
currentCaretIndex = preCaretRange.toString().length;
}
} else if ( (sel = doc.selection) && sel.type != "Control") {
var textRange = sel.createRange();
var preCaretTextRange = doc.body.createTextRange();
preCaretTextRange.moveToElementText(editor);
preCaretTextRange.setEndPoint("EndToEnd", textRange);
currentCaretIndex = preCaretTextRange.text.length;
}
return currentCaretIndex;
}
function getCurrentNodeIndexAtCaret(caretIndex) {
if (caretIndex == 0) {
return 0;
}
var currentNodeIndex = -1;
for (var i = 0; i < editor.childNodes.length; i++) {
var frontTextLength = getTextLengthFromStartTo(i);
var backTextLength = getTextLengthFromStartTo(i + 1);
if (caretIndex > frontTextLength && caretIndex <= backTextLength) {
currentNodeIndex = i;
break;
}
}
return currentNodeIndex;
}
function getCurrentTextIndexInNodexAtCaret(nodeIndex, caretIndex) {
var textLength = getTextLengthFromStartTo(nodeIndex);
var textIndex = caretIndex - textLength;
return (textIndex < 0) ? 0 : textIndex;
}
function clearSelection() {
if (window.getSelection().rangeCount > 0) {
if (lastRange != null) {
window.getSelection().removeAllRanges();
lastRange.collapse(true);
window.getSelection().addRange(lastRange);
}
}
}
function getTextNodesIn(node) {
var textNodes = [];
if (node.nodeType == 3) {
textNodes.push(node);
} else {
var children = node.childNodes;
for (var i = 0, len = children.length; i < len; ++i) {
textNodes.push.apply(textNodes, getTextNodesIn(children[i]));
}
}
return textNodes;
}
function setSelectionRange(el, start, end) {
if (document.createRange && window.getSelection) {
var range = document.createRange();
range.selectNodeContents(el);
var textNodes = getTextNodesIn(el);
var foundStart = false;
var charCount = 0, endCharCount;
for (var i = 0, textNode; textNode = textNodes[i++]; ) {
endCharCount = charCount + textNode.length;
if (!foundStart && start >= charCount && (start < endCharCount || (start == endCharCount && i <= textNodes.length))) {
range.setStart(textNode, start - charCount);
foundStart = true;
}
if (foundStart && end <= endCharCount) {
range.setEnd(textNode, end - charCount);
break;
}
charCount = endCharCount;
}
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
lastRange = range;
sel.anchorNode.parentElement.scrollIntoView();
} else if (document.selection && document.body.createTextRange) {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(true);
textRange.moveEnd("character", end);
textRange.moveStart("character", start);
textRange.select();
}
}
function findText(text, caseSensitive) {
var currentCaretIndex = getCurrentCaretIndex();
clearSelection();
var regex;
if (caseSensitive) {
regex = text;
} else {
regex = new RegExp(text, "gi");
}
var childNodes = editor.childNodes;
var startNodeIndex = getCurrentNodeIndexAtCaret(currentCaretIndex);
var endNodeIndex = childNodes.length;
var startTextIndex = 0;
if (window.getSelection().focusOffset == 1) {
startTextIndex = lastSearchedTextIndex + 1;
} else {
startTextIndex = getCurrentTextIndexInNodexAtCaret(startNodeIndex, currentCaretIndex);
}
var searchedTextIndex = -1;
var searchedNodeIndex = 0;
var searchTargetSentence = null;
var searchLoopCount = 0;
if (currentCaretIndex == editor.textContent.length) {
startNodeIndex = 0;
startTextIndex = 0;
}
do
{
for (var i = startNodeIndex; i < endNodeIndex; i++) {
if (typeof (childNodes[i].textContent) == undefined || childNodes[i].textContent == null) {
startTextIndex = 0;
continue;
}
if (startTextIndex == childNodes[i].textContent.length) {
startTextIndex = 0;
continue;
}
if (startTextIndex > 0) {
searchTargetSentence = childNodes[i].textContent.substring(startTextIndex, childNodes[i].textContent.length);
} else {
searchTargetSentence = childNodes[i].textContent;
}
searchedTextIndex = searchTargetSentence.search(regex);
if (searchedTextIndex > -1) {
searchedTextIndex += startTextIndex;
searchedNodeIndex = i;
break;
}
startTextIndex = 0;
}
if (searchedTextIndex == -1) {
endNodeIndex = startNodeIndex + 1;
startNodeIndex = 0;
searchLoopCount++;
}
} while (searchLoopCount < 2 && searchedTextIndex == -1);
lastsearchedNodeIndex = searchedNodeIndex;
lastSearchedTextIndex = searchedTextIndex;
if (searchedNodeIndex > -1 && searchedTextIndex > -1) {
var textStartIndex = getTextLengthFromStartTo(searchedNodeIndex) + searchedTextIndex;
setSelectionRange(editor, textStartIndex, textStartIndex + text.length);
return true;
} else {
return false;
}
}
function replaceText(textToFind, textToReplace, caseSensitive) {
if (findText(textToFind, caseSensitive) == true) {
var sel, range;
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
range.insertNode(document.createTextNode(textToReplace));
}
} else if (document.selection && document.selection.createRange) {
range = document.selection.createRange();
range.text = textToReplace;
}
return true;
} else {
return false;
}
}
There are no replace text method in CefSharp.
I think you have two options
Implement in javascript/html in the browser (DIY or something
like Tiny)
Manipulating the html from C# using GetSourceAsync and LoadHtml (Documentation)
Second question - I think you may be able to at least read the hits using the FindHandler. Have not tested this myself.

Is their a way to find the closest number to 0 in an Array?

I've got an array like that
var tideArray = new Array();
tideArray.push({tide:"haute1", difference: "-14"});
tideArray.push({tide:"haute2", difference: "-3"});
tideArray.push({tide:"basse1", difference: "-9"});
tideArray.push({tide:"basse2", difference: "4"});
tideArray.sortOn("difference", Array.NUMERIC);
trace(tideArray[0].tide);
For now, it's choosing the minimum number (-14) but I'd like to choose the closest number to 0.
Is there a way to do that ?
EDIT
I've tried that :
trace(closestToZero(tideArray));
function closestToZero(a:Array):int
{
var curDelta:int = Math.abs(0 - a[0].difference);
var curIndex:int = 0;
for(var i:int = 1; i < a.length; i++){
var thisDelta:int = Math.abs(0 - a[i].difference);
if(thisDelta < curDelta){
curIndex = i;
}
}
return curIndex;
}
But it seems that there is a mistake somewhere because the trace result is 3 (so it means that it's telling me that "basse2" (4) is the closest to 0... But, as you can see, it's "haute2" (-3) the closest).
I think it would be more efficient to simply loop over the array to find the item with the (absolute) minimum difference value:
if (tideArray.length > 0)
{
var minItem: Object = tideArray[0];
for (var index:int = 1; index < tideArray.length; index++)
{
if (Math.abs(tideArray[index].difference) < Math.abs(minItem.difference))
{
minItem = tideArray[index];
}
}
trace(minItem.tide);
}
Something like this
var tideArray = new Array();
...
function sortMyArray(a,b):int {
if (Math.abs(a) < Math.abs(b)) {
return -1;
}
if (Math.abs(a) > Math.abs(b)) {
return 1;
}
return 0;
}
tideArray.sort(sortMyArray);
Edit :
For your array.
function sortMyArray(a,b):int {
if (Math.abs(a.difference) < Math.abs(b.difference)) {
return -1;
}
if (Math.abs(a.difference) > Math.abs(b.difference)) {
return 1;
}
return 0;
}

.splice is not a function

I have this filter, and I have error filtered[i].splice is not a function
.filter('tsFilter', function() {
return function(model, filter) {
var filtered = [];
if (model != null) {
for (var i = 0; i < model.length; i++) {
filtered[i] = model[i];
}
}
for (i = 0; i < filtered.length; i++) {
if (filtered[i].List.length == 0)
filtered[i].splice(i, 1);
}
return filtered;
};
})
Why I have this error? how to solve that?
Here you have to update
for (i = 0; i < filtered.length; i++) {
if (filtered[i].List.length == 0)
filtered.splice(i, 1); //updated
}
Hope this will help you out.
It looks like you want to use:
for (i = 0; i < filtered.length; i++) {
if (filtered[i].List.length == 0)
filtered.splice(i, 1);
}
Although it is not a good idea to remove elements from the array that you are iterating over.

AS3 Array object showing up by ordering

I have 3 MC on stage which are all alpha=0
var mcArray:Array = [mc1,mc2,mc3];
for (var j:int = 0; j < mcArray.length; j++)
{
mcArray[j].alpha = 0;
}
I have one button which once I click then it will make 1 of the MC become alpha=1
revealBtn.buttonMode = true;
revealBtn.useHandCursor = false;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);
function revealClick(event:MouseEvent):void
{
for (var j:int = 0; j < mcArray.length; j++)
{
mcArray[j].alpha = 1;
}
}
But with the script above it will makes all 3 MC become alpha=1.
I know that this can achieve by using below code:
if(mc1.alpha!=1){
mc1.alpha=1
}
if(mc2.alpha!=1){
mc2.alpha=1
}
if(mc3.alpha!=1){
mc3.alpha=1
}
this code will give what I want to achieve but if there is more than 3 MC the lines of script will be longer.
revealBtn.buttonMode = true;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);
var mcArray:Array = [mc0,mc1,mc2];
function revealClick(event:MouseEvent):void
{
for(var i:uint = 0; i<mcArray.length; i++){
if(this['mc'+i].alpha !== 1){
this['mc'+i].alpha = 1;
break;
}
}
}
No that if statement would still turn all three to alpha 1.
Which of the three do you want to set to alpha = 1 when the click is made?
You could use something like this to set alpha = 1 on the first mc in the array which does NOT have alpha = 1
function revealClick(event:MouseEvent):void {
for (var j:int = 0; j < mcArray.length; j++){
if ( mcArray[j].alpha != 1 ){
mcArray[j].alpha = 1;
return;
}
}
}
Just use a counter.
var cnt : int = -1;
function revealClick(event:MouseEvent):void
{
if(++cnt < mcArray.length) mcArray[cnt].alpha = 1;
}

AS3: which is faster, a for loop or the forEach() array function?

I'm wondering which is faster in AS3:
array.forEach( function(v:Object, ...args):void
{ ... } );
Or
var l:int = array.length;
for ( var i:int = 0; i < l; i++ ) { ... }
for i :)
var array:Array = [];
for (var i:int=0; i < 100000; i++)
{
array[i] = i;
}
var time:uint = getTimer();
array.forEach( function(v:Object, ...args):void
{ v = 1; } );
trace(getTimer()-time); //trace 85
time = getTimer();
var l:int = array.length;
for ( i = 0; i < l; i++ ) { array[i] = 0; }
trace(getTimer()-time); //trace 3
The above answers do not take into account that you mostly will be performing operations on the array elements in the loop
This code
import flash.utils.getTimer;
var a:Array=[];
var time:int=0;
for(var i:int=0; i<10000; i++) {
a.push(new Date());
}
time=getTimer();
for(var j:int=0; j<a.length; j++) {
var dt:Date=a[j];
dt.valueOf();
}
trace("for: " , getTimer()-time);
time=getTimer();
for each(var xt:Date in a) {
xt.valueOf();
}
trace("for each: " , getTimer()-time);
time=getTimer();
a.forEach(nothing);
trace("a.forEach: " , getTimer()-time);
function nothing(d:Date, ...args):void {
d.valueOf();
}
Returns the following:
for: 3
for each: 2
a.forEach: 13
For 100000 values, the results are starker still
for: 27
for each: 17
a.forEach: 138
Overall winner: the for each loop
for each(var d:myClass in myCollection) {
//myCode
}
For VS Foreach on Array performance (in AS3/Flex)
hope this will help you in understanding the difference between for and for-each loop.
var time:uint;
var vec:Vector.<Number> = new Vector.<Number>;
for (var b:int=0; b < 1000000; b++)
{
vec[b] = 99;
}
///
time = getTimer();
for (var i:int = 0; i < vec.length; i++ )
{
vec[i] = 2;
}
trace('for i: '+(getTimer()-time)+'ms');
///
time = getTimer();
for (var j:int = vec.length - 1; j >= 0; --j)
{
vec[j] = 3;
}
trace('for i rev: '+(getTimer()-time)+'ms');
///
time = getTimer();
for ( var k:int in vec)
{
vec[k] = 4;
}
trace('for: '+(getTimer()-time)+'ms');
///
time = getTimer();
for each(var o:Number in vec)
{
o = 5;
}
trace('for each: '+(getTimer()-time)+'ms');
///
time = getTimer();
vec.forEach( function(v:int, ...args):void
{
v = 6;
}
);
trace('forEach: '+(getTimer()-time)+'ms');
// for i: 81ms
// for i rev: 79ms
// for: 28ms
// for each: 33ms
// forEach: 530ms

Resources