I've been working on this all day and just cannot seem to figure out why it won't work. I am grabbing a delimited string from a hidden field. I need to test to see if a string is contained in that original string. The simple example below should work but does not.
var orgStr = "091300159|091409568|092005411";
var newArr = orgStr.split('|');
console.log(orgStr);
console.log(newArr);
console.log("inarray? " + $.inArray(newArr, "092005411"));
It seems to work if I can wrap quotes around each value but all attempts are unsuccessful.
In JQuery's inArray function the value needs to come before the array.
console.log("inarray? " + $.inArray("092005411", newArr));
You could also use the native indexOf operator as such:
console.log("inarray? " + newArr.indexOf("092005411"));
Both should output "inarray? 2" to the console.
Have a look at the $.inArray docs.
The first argument is the value and the second the array. You did the opposite.
$.inArray("092005411", newArr) correctly returns 2.
Related
Okay, so I have a filepath with a variable prefix...
C:\Users\susan ivey\Documents\VKS Projects\secc-electron\src\views\main.jade
... now this path will be different for whatever computer I'm working on...
is there a way to traverse the string up to say 'secc-electron\', and drop it and everything before it while preserving the rest of it? I'm familiar with converting strings to arrays to manipulate elements contained within delimiters, but this is a problem that I have yet to come up with an answer to... would there be some sort of regex solution instead? I'm not that great with regex so I wouldn't know where to begin...
What you probably want is to do a split (with regex or not):
Here's an example:
var paragraph = 'C:\\Users\\susan ivey\\Documents\\VKS Projects\\secc-electron\\src\\views\\main.jade';
var splittedString = paragraph.split("secc-electron"); // returns an array of 2 element containing "C:\\Users\\susan ivey\\Documents\\VKS Projects\\" as the first element and "\\src\\views\\main.jade" as the 2nd element
console.log(splittedString[1]);
You can have a look at this https://www.w3schools.com/jsref/jsref_split.asp to learn more about this function.
With Regex you can do:
var myPath = 'C:\Users\susan ivey\Documents\VKS Projects\secc-electron\src\views\main.jade'
var relativePath = myPath.replace(/.*(?=secc-electron)/, '');
The Regex is:
.*(?=secc-electron)
It matches any characters up to 'secc-electron'. When calling replace it will return the last part of the path.
You can split the string at a certain point, then return the second part of the resulting array:
var string = "C:\Users\susan ivey\Documents\VKS Projects\secc-electron\src\views\main.jade"
console.log('string is: ', string)
var newArray = string.split("secc-electron")
console.log('newArray is: ', newArray)
console.log('newArray[1] is: ', newArray[1])
Alternatively you could use path.parse(path); https://nodejs.org/api/path.html#path_path_parse_path and retrieve the parts that you are interested in from the object that gets returned.
I've seen this question asked a few times but i'm getting back some weird results.
Does split() in javascript convert a string into an array?
Basically, onClick I console.log - $(this).attr('class')
I get the classes from my element which is styled_main selected_origin. I want to append the second class to another element so I do this : $(this).attr('class').split("styled_main ")
However, I get back an array that looks like ["", "selected_origin"] I'm trying to get back just the string of which should look like : selected_origin
You could use instead:
this.classList[1]
See support for classList
You could split your string by space and it will return ["styled_main", "selected_origin"] then get the second column (index 1) like :
$(this).attr('class').split(" ")[1];
//OR
$(this).attr('class').split(/\s+/)[1];
Hope this helps.
alert( "styled_main selected_origin".split(/\s+/)[1] );
I am trying to fetch numeric value from link like this.
Example link
/produkt/114664/bergans-of-norway-airojohka-jakke-herre
So I need to fetch 114664.
I have used following jquery code
jQuery(document).ready(function($) {
var outputv = $('.-thumbnail a').map(function() {
return this.href.replace(/[^\d]/g, '');
}).get();
console.log( outputv );
});
https://jsfiddle.net/a2qL5oyp/1/
The issue I am facing is that in some cases I have urls like this
/produkt/114664/bergans-of-norway-3airojohka-3jakke-herre
Here I have "3" inside text string, so in my code I am actually getting the output as "11466433" But I only need 114664
So is there any possibility i can get numeric values only after /produkt/ ?
If you know that the path structure of your link will always be like in your question, it's safe to do this:
var path = '/produkt/114664/bergans-of-norway-airojohka-jakke-herre';
var id = path.split('/')[2];
This splits the string up by '/' into an array, where you can easily reference your desired value from there.
If you want the numerical part after /produkt/ (without limitiation where that might be...) use a regular expression, match against the string:
var str = '/produkt/114664/bergans-of-norway-3airojohka-3jakke-herre';
alert(str.match(/\/produkt\/(\d+)/)[1])
(Note: In the real code you need to make sure .match() returned a valid array before accessing [1])
Here is the issue:
I go this Code:
var str = {"Acc":10 , "adm_data":"Denied"};
When I do something like:
console.log(str.Acc.match(/[0-9]+/g)) // To Get the Integer Value from the "Acc" key
Firebug Screams:
TypeError: str.Acc.match is not a function
console.log(str.Acc.match(/[0-9]+/g));
See Image:
I always do something like:
var str = "Hello _10";
console.log(str.match(/[0-9]+/g)) // This Works
Why is the Object thingi not working?
PLEASE NOTE:
As mentioned by #FabrÃcio Matté. The issue was that I was trying to
pass an integer Value to the .match method which does not belong
to integers. The solution was to do what #kundan Karn Suggested. Something like:
str.Acc.toString().match(/[0-9]+/g)// Converting it first to string then match. It worked!
match function works with string. So convert it to string first
str.Acc.toString().match(/[0-9]+/g)
It works just fine: http://jsfiddle.net/nKHLy/
but in order to get rid of the error you might want to try:
var str = {"Acc":"Hello_10" , "adm_data":"Denied"};
console.log(String(str.Acc).match(/[0-9]+/g));
or
var str = {"Acc":"Hello_10" , "adm_data":"Denied"};
console.log(str.Acc.toString().match(/[0-9]+/g));
To know the difference between the 2 options, check: What's the difference between String(value) vs value.toString()
I have this code, that takes a string and splits it into an array:
nodes = $("#" + model_id + "-" + node_class + "--" + "title").data("nodes").split(",")
When there is only one element in the string (no commas), the variable "nodes" does not become an array, but a regular variable. So when I try to iterate over each element in "nodes", nothing happens if the original string only contains one element. If it has several elements, everything is OK.
$.each(nodes, function (id, node_id) {
if ($("#" + model_id + "-" + node_class + "-" + node_id + "-" + "chkbx").is(":checked")) {
counter ++
}
})
I have tried to declare "nodes" as an array, but when I assign the splitted string, it's all the same. Since I use a "split" to assign values I don't think I can use "push" to append values to the array.
I have tried to put square brackets everywhere, I think, i.e. like this:
[nodes] = $("#" + model_id + "-" + node_class + "--" + "title").data("nodes").split(",")
... but that didn't help.
Is there any solutions to this, except from checking if "nodes" is an array or not, and then write different code to handle both options?
When there is only one element in the string (no commas), the variable "nodes" does not become an array, but a regular variable.
A) Variables referring to arrays are regular variables. B) That's not how split works. split always returns an array. If the delimiter isn't present in the string, the resulting array is one element in length. (Live proof) So as long as data returns a string, nodes will be set to an array. Note, though, that data does not always return a string, and so data("nodes").split(",") may fail with the error that split is not a function, because data can return null or an object as well as string. If you know it will always be a string because of your application logic, that's fine, but if so nodes will always be an array, which is why I mention it.
Re your comment below: Iteration works just fine on single-element arrays: http://jsbin.com/ehucop/2
I suspect you need to look at the JavaScript console in your browser, I bet you'll find an error occurring which is preventing your iteration code from running at all.