I want Require to Specific Part of string using Jquery or JavaScript?
Example :
var name = "jayesh.photo.png";
var name2 = "ruchita.photo.jpeg";
var name3 = "anil.photo.jpg";
How to get image extension using Jquery or JS.
I want ext1 = png, ext2 = jpeg, ext3 = jpg ;
You can use split() with .(dot) as delimitor and take the last array element after split.
var name = "jayesh.photo.png";
var name2 = "ruchita.photo.jpeg";
var name3 = "anil.photo.jpg";
var nameArray = name.split('.');
alert(nameArray[nameArray.length-1]);
var name2Array = name2.split('.');
alert(name2Array[name2Array.length-1]);
var name3Array = name3.split('.');
alert(name3Array[name3Array.length-1])
You could achieve this multiple ways:
var name = "jayesh.photo.png";
//1
console.log(name.replace(/^.*?\.([a-zA-Z0-9]+)$/, "$1"));
//2
console.log(name.substring(name.lastIndexOf(".")+1, name.length));
//3
var testvar = name.split('.');
console.log(testvar[testvar.length-1]);
//4
console.log(name.split('.').pop());
You can use String functions like below:
var required_string = name.substring(name.lastIndexOf(".")+1,name.length());
You could populate an array and loop it:
var name = "jayesh.photo.png";
var name2 = "ruchita.photo.jpeg";
var name3 = "anil.photo.jpg";
var photo = [name, name2, name3];
$.each(photo, function(key, value) {
console.log( "extension = " + value.substring(value.lastIndexOf("."),value.length ) );
});
Related
I have array object like this below
var TTP01[2,0,0,0,0,0,4,6,1,4,0,9,1]
If I assign TTP01[0] like this, I will get Output 2. This is working fine.
But I'm getting values separately and I need to assign the Object.
object = TTP;
count =01;
xy = x*y;
I concat like this below
var obj = objname.concat(count, "[", xy, "]");
console.log( obj );
In console log, I'm getting like this TTP01[0].
But want to get output 2
Please help me... Thanks
This will work.
eval(objname + count)[xy]
fullcode:
var TTP01 = [2,0,0,0,0,0,4,6,1,4,0,9,1];
var objname = "TTP";
var count = "01";
var xy = 0;
console.log(eval(objname + count)[xy]); // 2
You can try like this way,
var TTP01 = [2,0,0,0,0,0,4,6,1,4,0,9,1];
var objname = 'TTP';
var count = '01';
xy = 0;
var obj = window[objname + count];
console.log( obj[xy] );
Assign TTP01 to some base object :
var base = {
TTP01: [2,0,0,0,0,0,4,6,1,4,0,9,1]
}
var objname = 'TTP';
var count = '01';
var objStr = objname + count;
var xy = 0;
console.log(base[objStr][xy])
In the application a csv or xlsx file is parsed and the user then selects which fields represent an address.
After that I put the entire csv into an array and start to loop through it to send the address to be geocoded. The issue that I am having is that I need a way to set the array element equal to the field the user selected.
Before I loop through the array I grab the selection from each of the dropdowns like so.
var address_selection = document.getElementsByName("address1")[0].value;
var address2_selection = document.getElementsByName("address2")[0].value;
var city_selection = document.getElementsByName("city")[0].value;
var state_selection = document.getElementsByName("state")[0].value;
var postalcode_selection = document.getElementsByName("postalcode")[0].value;
My current code is setup like this, which hard codes the variable equal to a specific element.
for (i = 0; i < output.length; i++) {
var id = output.ID;
var address = output[i].Address;
var address2 = output[i].Address2;
var city = output[i].City;
var state = output[i].StateProvince;
var postalcode = output[i].ZipPostalcode;
What I need to do is somehow keep the variable names the same but change the element based on the user selection. I have tried something like this but it does not work.
var address = ("output[i]." + address_selection);
You can reference array indexes and object properties using [] notation, so in JS, this is very easy.
In your case, it looks like output is an array of objects, so output[i] is an object with fields like Address, Address2 etc. Instead of writing output[i].Address you can write it as output[i]["Address"]. Furthermore, "Address" can be replaced with a variable just like i.
With that in mind, and assuming the values of the options in your dropdownlists are things like "Address" and "Address2" then you should just be able to write this:
var address_selection = document.getElementsByName("address1")[0].value;
var address2_selection = document.getElementsByName("address2")[0].value;
var city_selection = document.getElementsByName("city")[0].value;
var state_selection = document.getElementsByName("state")[0].value;
var postalcode_selection = document.getElementsByName("postalcode")[0].value;
for (i = 0; i < output.length; i++) {
var id = output.ID;
var address = output[i][address_selection];
var address2 = output[i][address2_selection];
var city = output[i][city_selection];
var state = output[i][state_selection];
var postalcode = output[i][postalcode_selection];
var address_selection = document.getElementsByName("address1")[0].value;
var address2_selection = document.getElementsByName("address2")[0].value;
var city_selection = document.getElementsByName("city")[0].value;
var state_selection = document.getElementsByName("state")[0].value;
var postalcode_selection = document.getElementsByName("postalcode")[0].value;
var myArray = {
address_selection: address_selection,
address2_selection: address2_selection,
city_selection: city_selection,
state_selection: state_selection,
postalcode_selection: postalcode_selection
};
for (var key in myArray) {
console.log("key " + key + " has value " + myArray[key]);
}
I have data two sets of data as follows:
"One.Two.Three.Four"
"One.Two.Three.1.Four"
The first three parts are fixed and the remaining can extend to as many as possible.
I am trying to build an object where I want to split and combine whatever is present after three into an object.
var split = samplestr.split('.');
var finalarray = [];
if(split.length>4)
{
finalarray[0] = split[0];
finalarray[1] = split[1];
finalarray[2] = split[2];
finalarray[3] = split[3]+"."split[4];
}
I need to generalise this such that even if the string is of the form
"One.Two.Three.1.2.3.Four"
finalarray[3] = 1.2.3.Four;
Any hints on generalising this?
With Array#shift and Array#join.
var split = samplestr.split('.');
var finalarray = [];
if(split.length > 4) {
finalarray[0] = split.shift();
finalarray[1] = split.shift();
finalarray[2] = split.shift();
finalarray[3] = split.join(".");
}
simply replace
finalarray[3] = split[3]+"."split[4];
with
finalarray[3] = split.slice(3).join(".");
Split the string, slice the first part and append the join'ed second part:
console.info=function(x){document.write('<pre>'+JSON.stringify(x,0,3)+'</pre>')}
//--
var str = "One.Two.Three.Four.More.Stuff";
var s = str.split('.');
var result = s.slice(0, 3).concat(s.slice(3).join('.'));
console.info(result);
I have string like this:-
var src=url("http://localhost:200/assets/images/eyecatcher/6/black6.png)"
And now I want to get image name i.e black6.png and folder name 6.
I know there is substr function I can use but the file name and folder name will be dynamic like
orange12.png and 12 etc.
How I can get these values? Please help me.
Thanks
You can use split method for this:
var src = "http://localhost:200/assets/images/eyecatcher/6/black6.png";
var parsed = src.split( '/' );
console.log( parsed[ parsed.length - 1 ] ); // black6.png
console.log( parsed[ parsed.length - 2 ] ); // 6
console.log( parsed[ parsed.length - 3 ] ); // eyecatcher
etc.
If the base URL is always the same you could do
var url = "http://localhost:200/assets/images/eyecatcher/6/black6.png";
var bits = url.replace("http://localhost:200/assets/images/eyecatcher/", "").split("/");
var folder = bits[0], // 6
file = bits[1]; // black6.png
If your string is:
var src="http://localhost:200/assets/images/eyecatcher/6/black6.png"
Use the following:
var parts = src.split('/');
var img = parts.pop(); //black6.png
var flder = parts.pop(); //6
var sflder = parts.pop(); //eveatcher
You may try like this:
var str = myString.split('/');
var answer = str[str.length - 1];
var answer1 = str[str.length - 2];
var answer2 = str[str.length - 3];
var img_name = src.split('/')[7];
var folder_name = src.split('/')[6];
Using split & slice, Say like bellow
var src = "http://localhost:200/assets/images/eyecatcher/6/black6.png";
var arr = src.split('/').slice(-2) //returns ["6", "black6.png"]
arr[0] //folderName
arr[1] //filename
var str = "http://MachineName:200/assets/images/eyecatcher/6/black6.png";
var newStr = str.split("/");
ubound = newStr.length;
fileName = newStr[ubound-1];
I have an array of string like below:
var array =[];
array.push("Complex12");
array.push("NumberCar1");
array.push("Protect5");
I want to split the string and number of each item.
var Id = parseInt(array[0].match(/\d/g));
var type = array[0].replace(/\d+/g, '');
But I only get Id = 1(I want 12) and type = "Complex", where am I wrong?
thanks
I think you just missed + in first regexp
var Id = parseInt(array[0].match(/\d+/g));
Do it in one pattern with capture groups:
var mystr = "Complex12";
if (m = mystr.match(/^([a-z]+)([0-9]+)$/i)) {
var type = m[1];
var id = m[2];
}