Lets say I have three arrays:
var arr1 = ['red', 'orange', 'yellow',];
var arr2 = ['car', 'bus'];
var arr3 = ['china'];
How to get the following combinations:
1. Order matters,
2. Each result(can be a array) should contain 0..1 element of each arr
3. Element of arr1,arr2,arr3 can appear 0..1 times :
red,car,china
red,china,car
car,red,china
car,china,red
china,car,red,
china,red,car
red,bus,china
red,china,bus
bus,red,china
bus,china,red
china,bus,red,
china,red,bus
orange,car,china
orange,china,bus
bus,orange,china
bus,china,orange
china,bus,orange,
china,orange,bus
orange,bus,china
orange,china,bus
bus,orange,china
bus,china,orange
china,bus,orange,
yellow,car,china
yellow,china,car
car,yellow,china
car,china,yellow
china,car,yellow,
china,yellow,car
yellow,bus,china
yellow,china,bus
bus,yellow,china
bus,china,yellow
china,bus,yellow,
china,yellow,bus
red,car
car,red
red,bus
bus,red
orange,car
car,orange,
orange,bus
bus,orange,
yellow,car
car,yellow,
yellow,bus
bus,yellow,
red,china
china,red,
orange,china
china,orange,
yellow,china
china,yellow,
car,china
china,car,
bus,china
china,bus,
red,
orange,
yellow,
car,
bus,
china,
What I could not figure out is how to control the appear times in the element of each arr, my code is as follows:
function get_titles_appearances(titles_columns) {
titles_columns = titles_columns || [];
var n = titles_columns.length;
if (n === 0) { return [] }
if (n === 1) { return titles_columns[0] }
var save = function () {
var m = groups.length, c = [];
while (m--) { c[m] = groups[m] }
rtn.push(c);
}
var i = 0, len = 0, counter = [], groups = [], rtn = [];
for (; i < n; i++) {
counter[i] = 0;
groups[i] = titles_columns[i][0];
}
save();
while (true) {
i = n - 1, len = titles_columns[i].length;
if (++counter[i] >= len) {
while (counter[i] >= len) {
if (i === 0) { return rtn }
groups[i] = titles_columns[i][0];
counter[i--] = 0;
counter[i]++;
len = titles_columns[i].length;
}
}
groups[i] = titles_columns[i][counter[i]];
save();
}
}
_.forEach(get_titles_appearances(titles_columns_config_default), function (value) {
// titles_appearances.push( _.join(value, '')+"':'"+_.join(value, ','))
titles_appearances = titles_appearances+"{"+ _.join(value, '')+":"+_.join(value, ',')+"},"
})
titles_columns_config_default refer to [arr1,arr2,arr3]
Related
Need to format my array for chart purpose
myArr=[["6709"],["1949"],["87484"],["12760"],["13326"],["3356"],["98000"],["16949"],["29981"],["7879"],["117640"],["30727"],["122071"],["21325"],["210406"],["65824"],["2744807"],["56664"],["382719"],["134578"],["2440528"],["83819"],["1362744"],["450092"],["2461"],["336"],["166446"],["16363"]]
Below Formatted Array
formatArr= [["6709", "1949", "87484", "12760"], ["13326", "3356", "98000", "16949"], ["29981", "7879", "117640", "30727"], ["122071", "21325", "210406", "65824"], ["2744807", "56664", "382719", "134578"] ["2440528", "83819", "1362744", "450092"], ["2461", "336", "166446", "16363"]]
You could reduce it like this for example:
const formatArr: string[][] = myArr.reduce((prev, item, index) => {
if (index % 4 === 0) {
// every fourth item creates a new array with the current item:
prev.push(item);
} else {
// every other item pushes to the previously added item:
prev[prev.length - 1].push(item[0]);
}
return prev;
}, [] as string[][]);
myArr = [["6709"],["1949"],["87484"],["12760"],["13326"],["3356"],["98000"],["16949"],["29981"],["7879"],["117640"],["30727"],["122071"],["21325"],["210406"],["65824"],["2744807"],["56664"],["382719"],["134578"],["2440528"],["83819"],["1362744"],["450092"],["2461"],["336"],["166446"],["16363"]]
formatArr = []
function makeArray(params) {
let element = [];
for (let i = 0; i < myArr.length; i++) {
element.push(myArr[i][0])
if ((i + 1) % 4 == 0) {
formatArr.push(element);
element = [];
}
}
console.log(formatArr);
}
makeArray();
This is correct if myArr has 4n element, if not use this
function makeArray(params) {
let element = [];
for (let i = 0; i < myArr.length; i++) {
element.push(myArr[i][0])
if ((i + 1) % 4 == 0) {
formatArr.push(element);
element = [];
}
if (i == myArr.length - 1 && (i + 1) % 4 != 0) {
formatArr.push(element);
}
}
console.log(formatArr);
}
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.
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) );
does anyone have any idea what to do in order to find the number of sequences in an array?
for example, my array is:
var numbers:Array = new Array(banana, banana, apple, banana, banana);
and i need to find is:
* how many times there is a sequence of "banana"
* and the length of each sequence.
what shell i do in order to get the following result:
2,1,2 (2 bananas, 1 apple, 2 bananas)
i tried with do while loop, but i i guess i miss something.
a short example will be very appreciated!
thanx
var prev:String = null;
var q:int = 0;
var result:Array = new Array();
for(var i:int=0; i<numbers.length; ++i){
if(prev!=numbers[i]){
if(q>0) result.push(q);
q=1;
prev=numbers[i];
}
else ++q;
}
if(q>0) result.push(q);
This is, assuming banana, etc. are strings (probably a typo above?). It would be simple to modify to other types of objects
Really all you want to know is whether the string at index n equals the string at index n+1...
var targetIndex:int = numbers.length - 1;
var results:Array = [1];
var resultsIndex:int = 0;
for(var n:int = 0; n < targetIndex; n++) {
if(numbers[n] == numbers[n+1]) {
results[resultsIndex]++;
} else {
results[++resultsIndex] = 1;
}
}
trace(results.join(','));
function sequencesInArray(array:Array):Array {
var sequence:Array = [];
var currSequenceCount:uint = 1;
for (var i:uint = 1; i < numbers.length; i++) {
if (numbers[i - 1] != numbers[i]) {
sequence.push(currSequenceCount);
currSequenceCount = 1;
} else {
currSequenceCount++;
}
}
return sequence;
}
Then:
var banana:int = 1;
var apple:int = 2;
sequencesInArray([banana, banana, apple, banana, banana]); //returns: [2, 1, 2]
In the question you don't define banana and apple, anyway I would use a map or a Dictionary to store key/value pairs, with the key being the string/object you want to count and the value being the counter of the object occurences in your array.
var objectsCounter:Dictionary = new Dictionary();
for (var key:String in numbers)
{
if ( objectsCounter[key] )
objectsCounter[key] = objectsCounter[key] + 1;
else
objectsCounter[key] = 1;
}
This way you can store any type in the dictionary.
edit:
for (var key:String in objectsCounter)
{
// iterates through each object key
}
for each (var value:Number in objectsCounter)
{
// iterates through each value
}
I believe this is what you're looking for:
var array:Array = [
"banana", "banana",
"apple",
"banana", "banana", "banana"
];
var sequences:Array = findSequences(array, "banana");
trace("sequences:", sequences); // prints "sequences: 2,3"
And:
private function findSequences(array:Array, searchElement:*):Array
{
var sequences:Array = [];
var currentSequence:int = 0;
for each (var element:* in array) {
if (element == searchElement) {
currentSequence++;
} else if (currentSequence > 0) {
sequences.push(currentSequence);
currentSequence = 0;
}
}
if (currentSequence > 0) {
sequences.push(currentSequence);
}
return sequences;
}
I have an array of objects. Each object has a color attribute which could be "red", "blue", "yellow", "green", "orange" or "purple". There are 20-30 objects in the array so colors repeat. My goal is to sort the array so that no colors are next to each other. Distribution of colors is not exactly even but close.
This is what I have so far. It checks the next and previous object for a color match and if it finds a match it moves it to the end of the array.
private function sortColors():void
{
var getNext:uint;
var getPrev:uint;
var maxCount:uint = colorArray.length;
for (var i:uint = 0; i < maxCount; i++) {
var cur:ValueObject = colorArray[i];
(i == maxCount-1) ? getNext = 0 : getNext = i+1;
(i == 0) ? getPrev = maxCount-1 : getPrev = i-1;
var next:ValueObject = colorArray[getNext];
var prev:ValueObject = colorArray[getPrev];
if (cur.color == next.color) {
var move:ValueObject = colorArray[getNext];
colorArray.splice(getNext, 1);
colorArray.push(move);
}
if (cur.color == prev.color) {
var move:ValueObject = colorArray[getPrev];
colorArray.splice(getPrev, 1);
colorArray.push(move);
}
}
}
This works OK but if there is more of a certain color they end up repeating at the end. I could add something to the end to throw those back into the mix but I feel like there must be a better way. Someone enlighten me.
Try:
var colorObjects:Array = [/* list of objects with colors - populated below*/];
var jumbled:Array = [];
var lastColor:String = "";
function getDifferentTile():void
{
if(lastColor.length == 0)
{
jumbled.push(colorObjects.pop());
lastColor = jumbled[0].mycolor;
}
else
{
var i:Object;
for each(i in colorObjects)
{
var repeat:uint = 0;
if(i.mycolor != lastColor)
{
jumbled.push(i);
lastColor = i.mycolor;
colorObjects.splice(colorObjects.indexOf(i), 1);
return;
} else {
repeat++;
}
if (repeat > 0 && repeat == colorObjects.length) {
jumbled.push(i);
colorObjects.splice(colorObjects.indexOf(i), 1);
return;
}
}
}
}
// list of random colors
var colors:Array = ["0x000000","0x444444","0xFFFFFF","0xFF00FF"];
// prepare random array for test
var i:uint = 0;
for(i; i<100; i++)
{
var obj:Object =
{
mycolor: colors[uint(Math.random()*colors.length)]
};
colorObjects.push(obj);
}
// fill the jumble array until the original listing is empty
while(colorObjects.length > 0)
{
getDifferentTile();
}
// output jumbled
var j:Object;
for each(j in jumbled)
{
trace(j.mycolor);
}