HI ,
In Java Script ,
var a ="apple-orange-mango"
var b ="grapes-cheery-apple"
var c = a + b // Merging with 2 variable
var c should have value is "apple-orange-mango-grapes-cheery" .Duplicated should be removed.
Thanks ,
Chells
After your string is combined, you will want to split it using the delimiters (you can add these back in later).
example:
var a ="apple-orange-mango"
var b ="grapes-cheery-apple"
var c = a + "-" + b
var Splitted = c.split("-");
the Splitted variable now contains an array such as [apples,orange,mango,grapes,cherry,apple]
you can then use one of many duplicate removing algorithms to remove the duplicates. Then you can simply do this to add your delimiters back in:
result = Splitted.join("-");
Here's a brute force algorithm:
var a;
var b; // inputs
var words = split(a+b);
var map = {};
var output;
for( index in words ) {
if( map[ words[index] ]!=undefined ) continue;
map[ words[index] ] = true;
output += (words[index] + '-');
}
output[output.length-1]=' '; // remove the last '-'
The map acts as a hashtable.
Thats it!
I don't know if it is an homework.
By the way you can split strings like a and b with the split method of string object.
in your case:
firstArray=a.split("-");
secondArray=b.split("-");
the removal of duplicates is up to you...
In your simple example, just use var c = a + "-" + b;
If you want duplicates removed, split a and b into arrays, and combine them, like so:
var avalues = a.split("-");
var bvalues = b.split("-");
var combined = avalues.concat( bvalues );
// now loop over combined and remove duplicates
Related
Let's illustrate my question with some code to make you understand what I need.
I searched for a solution and they always propose to add the element of an array in another array. This is not what I want.
Example:
<script type="text/javascript">
var array_1 = [];
var array_2 = [1,2,3];
var array_3 = [4,5,6];
array_1.push(array_2);
array_1.push(array_3);
alert(array_1);
</script>
This will show :
1,2,3,4,5,6
I would like to get
[1,2,3],[4,5,6]
which means array_1 contains 2 elements : array_2 and array_3
I need that because I will loop array_1 which will print in the HTML the 3 elements of each array (array_2 and array3).
For example:
var main_array = [];
var title_1 = ["My title 1",16,"color:#000;"];
var title_2 = ["My title 2",14,"color:#333;"];
main_array.push(title_1);
main_array.push(title_2);
txt=""
main_array.forEach(myFunction);
document.getElementById("demo").innerHTML = txt;
function myFunction(value, index, array) {
txt = txt + '<span style=\'font-size:' + value[1] + 'color:' + value[2] + '\'>' + value[0] + '</span><br>';
}
I am showing this last example in case it doesn't exist the possibility to add an array inside an array in Javascript. Maybe someone will think of another solution to accomplish what I need to do.
You do get [[1,2,3],[4,5,6]]. It just doesn't seem like it since alert calls toString, which joins all the elements together with a comma. So, [[1,2,3],[4,5,6]].toString() == [1,2,3].toString() + "," + [4,5,6].toString() which is "1,2,3" + "," + "4,5,6" which is "1,2,3,4,5,6". Just console.log it and you'll see it's a nested array.
const array_1 = [1,2,3];
const array_2 = [4,5,6];
const array_3 = [[...array_1], [...array_2]];
So i have this string
first €999, second €111
Im trying to make an array that looks like this (numbers after every €)
999,111
Edit:
Yes i have tried to split it but wont work. i tried to look it up on google and found something with indexof but that only returned the number of the last €.
rowData[2].split('€').map(Number);
parseInt(rowData[2].replace(/[^0-9\.]/g, ''), 10);
split(rowData[2].indexOf("€") + 1);
The numbers are variable.
var input ="first €999, second €111";
var output=[];
var arr = input.split(",");
for(var i=0;i<arr.length;i++)
{
output.push(parseInt(arr[i]));
}
var output_string = output.stingify();
console.log(output); //Output Array
console.log(output_string); //Output String
If the numbers will always be of 3 digits in length, you can do this. If not, you need to specify a bit more.
var string = "€999, second €111";
var temp = [];
var digitArray = [];
temp = string.split(",");
for(var i=0;i<temp.length,i++){
digitArray.push(temp[i].substring(temp[i].indexOf("€"),3));
}
//digitArray now contains [999,111];
Edit, based on your requirement of variable digit lengths
var string = "€999, second €111, third €32342";
var temp = [];
var digitArray = [];
temp = string.split(",");
for(var i=0;i<temp.length,i++){
digitArray.push(temp[i].replace(/^\D+/g, '')); //Replace all non digits with empty.
}
//digitArray now contains [999,111,32342]
I have mathematical calculations in div tag, like that:
13*7=91
So how to split and parse data?
and it will stored in variables like that:
var A = 13;
var Operation = '*';
var B = 7;
var Result = 91;
please tell me how to make that :)
You can split it first by = sign, and then by possible math signs, for example:
var s = '13*7=91';
var a = s.split('=');
var b = a[0].split(/[\+\-\*\/\^%]/);
var A = b[0];
var B = b[1];
var Operation = a[0].replace(A,'').replace(B,'');
var Result = a[1];
console.log(A+Operation+B+'='+Result);
Output:
13*7=91
This is an easy way of doing it, simply using RegExp.
The first one is /[0-9]+/g to take the operands and the result numbers and the second one is /[0-9]+(.)[0-9]+/ to extract the operator, then I print the result in a diplay p elemnt:
var str = document.getElementById("calcul").innerText;
var re = /[0-9]+/g;
var re2 = /[0-9]+(.)[0-9]+/;
var operands = str.match(re);
var operator = str.match(re2)[1];
var A = operands[0];
var B = operands[1];
var result = operands[2];
var display = document.getElementById("display");
display.innerHTML = "var A = " + operands[0] + "<br>var B = " + operands[1] + "<br>var result = A" + operator + "B =" + result;
<div id="calcul">
13*7=91
</div>
<br>Calculation results :
<p id="display">
</p>
Maybe you can try something like this:
Make a regular expression that detects the numbers separated by anything (+, *, -, /, =, etc).
Make that regular expression detects the separating elements.
Then execute eval() in javascript. Be careful with this.
When you have a piece of code show us and we can help you better.
Good luck.
I have an array say var arr = [1,2,3,4,5,6,7,8]
Now I do a join like give below
arr.join("|");
My requirement is that this array should contain new line after each third element. Like
1|2|3
4|5|6
7|8|9
A help will be appreciated.
Thanks
arr.join("|").replace(/([^|]+?\|[^|]+?\|[^|]+?)\|/g, "$1\n");
One possible approach:
i.join('|').replace(/\|/g, function(){
var c = 0;
return function(str) {
return ++c % 3 ? str : '\n';
}
}());
replace essentially replaces each third | in the string with \n.
You should slice your array into multiple sub arrays which can individually be joined together:
var arr = [1,2,3,4,5,6,7,8,9];
// temporary array to push sliced and joined sub array into
var arr_ = [], i;
for (i = 0; i < arr.length; i = i + 3) {
// slice range of 3 element from arr, join and push into arr_
arr_.push(arr.slice(i, i + 3).join("|"));
}
// join by newline
arr_.join("\n");
You can extract 3 asvariable to vary the column width.
If you have array of digits:
arr.join("|").match(/(\d+\|\d+\|\d+)/g).join("\n");
friends.
I have an array and it contains some string values.
ex: array name="All_array"
Now i want to check all values in that array for first character of a string.
if a String starts with character 'a' then move that string to array called "A_array".
if a String starts with character 'b' then move that string to array called "B_array".
How to achieve this task.
var splitArrays = {};
for(var i = 0; i < All_array.length; ++i){
var firstChar = All_array[i].substr(0,1).toUpperCase();
if(!splitArrays[firstChar + '_array'])
splitArrays[firstChar + '_array'] = [];
splitArrays[firstChar + '_array'].push(All_array[i]);
}
This will take every element in All_array and put them into an object containing the arrays indexed by the first letter of the elements in All_array, like this:
splitArrays.A_array = ['Abcd','Anej','Aali']
etc...
Here's a fiddle: http://jsfiddle.net/svjJ9/
The code would be this:
for(var i=0; i<All_array.length; i++){
var firstChar = All_array[i].substr(0, 1).toUpperCase();
var arrayName = firstChar + "_array";
if(typeof(window[arrayName]) == 'undefined') window[arrayName] = []; //Create the var if it doesn't exist
window[arrayName].push(All_array[i]);
}
A_array = []; //empty the array (cause you wanted to 'move')
Hope this helps. Cheers
You could do it using each() and charAt:
$.each(All_array,function(i,s){
var c = s.charAt(0).toUpperCase();
if(!window[c + '_array']) window[c + '_array'] = [];
window[c + '_array'].push(s);
});