Unable to split same string for two times.What i am doing wrong?
// my original string
var str = "Left,Right-broken at left side";
var ary = "Left,Right-broken at left side";
//getting text after - (working fine)
var res = str.split("-").pop();
$('#disc_comm_tlside_ed').empty();
$('#disc_comm_tlside_ed').val(res);
// with this i can get text before -once again i need to splt by comma only
var myarrays= str.substr(0, str.indexOf('-'));
//even this also returning original string
var splt = ary.split(',');
// alert(splt) when i alert this getting full string
for(var i = 0; i < splt.length; i++)
{
//alert(myarray[i]);
$(this).find("option[value ='"+myarray[i]+"']").attr("selected",true);
$('.batch_ed').multiselect('rebuild');
}
Finally returning original string!!
Need to get text before-and split text separated by ,
need to get left and right only as array
Edit: adding the code snippet posted in the comment.
var elements = "";
var batchnoe= "";
$.each(data.response.frc_disloc, function (a,b){
batchnoe = '.batch_ed';
$(batchnoe).each(function(i, obj) {
var batch_splt = b.frac_side;
var myarray = batch_splt.split(',');
for(var i = 0; i < myarray.length; i++) {
$(this).find("option[value ='"+myarray[i]+"']").attr("selected",true); $('.batch_ed').multiselect('rebuild');
}
});
});
please check console logs for the output.
// my original string
var str = "Left,Right-broken at left side";
var ary = "Left,Right-broken at left side";
//getting text after - (working fine)
var res = str.split("-").pop();
$('#disc_comm_tlside_ed').empty();
$('#disc_comm_tlside_ed').val(res);
// with this i can get text before -once again i need to splt by comma only
var myarrays= str.substr(0, str.indexOf('-'));
//even this also returning original string
var splt = myarrays.split(',');
console.log(splt);
// alert(splt) when i alert this getting full string
for(var i = 0; i < splt.length; i++)
{
console.log(splt[i]);
$(this).find("option[value ='"+splt[i]+"']").attr("selected",true);
//$('.batch_ed').multiselect('rebuild');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="disc_comm_tlside_ed"></div>
Related
Would somebody here please show me the way to modify the regexp below so that I could then with it get multiple integers per array item? I can detect one integer like in the top-most str below using \d+. However, the function will error out with the other two examples, below it, str = "7yes9 Sir2", etc. Thank you.
//str = "10 2One Number*1*";
//output -> [10, 2One, Number*1*] -> [10 + 2 + 1] -> 13
var str = "7Yes9 Sir2";
//output -> NaN
//var str = "8pop2 1";
//output -> NaN
function NumberAddition(str) {
input = str.split(" ");
var finalAddUp = 0;
var finalArr = [];
for(var i = 0; i<=input.length-1; i++) {
var currentItem = input[i];
var regexp = /(\d+)/g;
finalArr.push(Number(currentItem.match(regexp)));
var itemToBeCounted = +finalArr[i];
finalAddUp += itemToBeCounted;
}
return finalAddUp;
}
console.log(NumberAddition(str));
Try sth. like
HTML
<span id="res"></span>
JS
var str = "7Yes9 Sir2";
var matches = str.match(/\d+/g);
var res=0;
for (var i=0; i< matches.length; i++) {
res += parseInt(matches[i],0);
}
$('#res').html(res);
See this working fiddle
Looking to extend my javascript object, I want to find the minium and maximum of a multicolumn csvfile. I have looked up solutions but I cannot really grasp the right way. I found a solution here: Min and max in multidimensional array but I do not get an output.
My code that I have for now is here:
function import(filename)
{
var f = new File(filename);
var csv = [];
var x = 0;
if (f.open) {
var str = f.readline(); //Skips first line.
while (f.position < f.eof) {
var str = f.readline();
csv.push(str);
}
f.close();
} else {
error("couldn't find the file ("+ filename +")\n");
}
for (var i=(csv.length-1); i>=0; i--) {
var str = csv.join("\n");
var a = csv[i].split(","); // convert strings to array (elements are delimited by a coma)
var date = Date.parse(a[0]);
var newdate = parseFloat(date);
var open = parseFloat(a[1]);
var high = parseFloat(a[2]);
var low = parseFloat(a[3]);
var close = parseFloat(a[4]);
var volume = parseFloat(a[5]);
var volume1000 = volume /= 1000;
var adjusted_close = parseFloat(a[6]);
outlet(0, x++, newdate,open,high,low,close,volume1000,adjusted_close); // store in the coll
}
}
Edit
What if, instead of an array of arrays, you use an array of objects? This assumes you're using underscore.
var outlet=[];
var outletkeys=['newdate','open','high','low','close','volume','volume1000','adjusted_close'];
for (var i=(csv.length-1);i>0; i--) {
var a = csv[i].split(",");
var date = Date.parse(a[0]);
var volume = parseFloat(a[5],10);
outlet.push( _.object(outletkeys, [parseFloat(date,10) , parseFloat(a[1],10) , parseFloat(a[2],10) , parseFloat(a[3],10) , parseFloat(a[4],10) , parseFloat(a[5],10) , volume /= 1000 , parseFloat(a[6],10) ]) );
}
Then the array of the column 'open' would be
_.pluck(outlet,'open');
And the minimum it
_.min(_.pluck(outlet,'open'));
Edit2
Let's forget about underscore for now. I believe you need to get the maximum value on the second column, which is what you put in your open variable.
¿Would it help if you could have that value right after the for loop? For example
var maxopen=0;
for (var i=(csv.length-1); i>=0; i--) {
var a = csv[i].split(",");
var date = Date.parse(a[0]);
var newdate = parseFloat(date);
var open = parseFloat(a[1]);
maxopen=(open>maxopen)? open : maxopen; // open overwrites the max if it greater
...
...
outlet(0, x++, newdate,open,high,low,close,volume1000,adjusted_close);
}
console.log('Maximum of open is',maxopen);
I have a gigantic list (800 items) and one really long string. I want to get the first item in the array that matches the part of the string and stored in a variable.
My code currently:
for (var i = 0; i<gigantic_genre_array.length; i++) {
var test_genre = thelongstr.indexOf(gigantic_genre_array[i]);
if(test_genre != -1) {
tag1 = gigantic_genre_array[test_genre];
alert(tag1);
}
}
This doesn't work like I thought it would, any suggestions?
Try this:
for(var i = 0; i<gigantic_genre_array.length; i++){
var test_genre = thelongstr.indexOf(gigantic_genre_array[i]);
if(test_genre!=-1){
tag1 = gigantic_genre_array[i];
alert(tag1);
}
}
Do the process reversely it will be efficient too.
var wordArray = thelongstr.split(' ');
for(var i=0,len = wordArray.length; i < len; i++)
{
if(gigantic_genre_array.indexOf(wordArray[i]) > -1)
{
alert(wordArray[i]);
}
}
You may create a RegExp based on the array and test it against the string:
var gigantic_genre_array=['foo','bar','foobar'];
var thelongstr='where is the next bar';
alert(new RegExp(gigantic_genre_array.join('|')).exec(thelongstr)||[null][0]);
//returns bar
I have an object in javascript -:
var array = {"1":"John","2":"Caprio","3":"David","4":"Edward"}
I want to do this -:
var message = 'Wats up David#1. Are you with David#5 or Caprio#89';
$.each(array, function(key, value){
var matchMe=value+'#'+anywholenumber;
if(message.match(matcheMe))
{
var ge = new RegExp(matchMe, 'g');
message = message.replace(ge,'['+matchMe+']');
} });
How do i do it? Thanks a lot for help
The code you have would replace every number with the first element (john) if it were working.
I think you want this:
var arr = array("John","Caprio","David","Edward","Suzy");
var message = 'Wats up David#1. Are you with David#5 or Caprio#2';
for (i=0; i < arr.length; i++) {
var re = new RegExp(i, 'g');
message = message.replace(re, arr[i]);
}
I have a function which has to change characters from one array to the characters from another. It is kind of simple encryption. I have:
var plainArray = ['A','B','C',...,'Z'];
var cipherArray = ['a','b','c',...,'z'];
function rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet)
already working.
Now I have to write a function which will change given word into encrypted word.
function encrypt(plainText, signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet)
{
var encryptedString = signalCharacter;
//i is what will hold the results of the encrpytion until it can be appended to encryptedString
var i;
// rotate array to signal character position
var rotateArray = rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet);
for (var count = 0; count < plainText.length; count++)
{
plainAlphabet = plainText.charAt(count);
i = cipherAlphabet[plainAlphabet];
encryptedString = encryptedString + rotateArray[i];
}
return encryptedString;
}
This function returns signal character and then a string of errors. Do you know what is wrong?
You are overwriting plainAlphabet with one character, thus discarding the alphabet. I guess that's not what you want.
However, you only posted the signature of rotateToPosition and not the actual code of it, so I cannot test my solution.
function encrypt(plainText, signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet) {
var encryptedString = signalCharacter;
//i is what will hold the results of the encrpytion until it can be appended to encryptedString
var i;
// rotate array to signal character position
var rotateArray = rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet);
for (var count = 0; count < plainText.length; count++)
{
var plainLetter = plainText.charAt(count);
i = cipherAlphabet[plainLetter];
encryptedString = encryptedString + rotateArray[i];
}
return encryptedString;
}