This question already has answers here:
How do I find an exact word in a string?
(5 answers)
Closed 28 days ago.
I want to compare the following two variables to see if they match exactly or not.
What I am doing right now is:
var string1 = "S1";
var string2 = "LS1 B26 M90";
let result = string2.indexOf(string1);
It returns 1 which means S1 exists in string2. I want it to look for "S1" and not to match with "LS1".
you can simply achive by below:
String("LS1 B26 M90").split(" ").includes("LS1")
String("LS1 B26 M90").split(" "): convert string into string list.
.includes("LS1"): will check the existence it return true in case of
match otherwise false.
This question already has answers here:
convert string array to integer array
(4 answers)
Closed 4 years ago.
I have a jQuery array ["3434", "3433"]
And I would like to make it like [3434, 3433]
No need for jQuery. Supposing you're sure they're all numbers (that is, we're not checking for errors), you can map them with Number, which converts them from string to numbers.
["3434", "3433"].map(Number);
Considering that Number returns NaN in case of error, you may then want to filter the result to remove undesired elements.
let nums = ["3434", "3433", "foo"].map(Number).filter(n => !isNaN(n));
console.log(nums);
This question already has answers here:
How to get character array from a string?
(14 answers)
Closed 6 years ago.
Suppose I have the following array-
var x= ["hello"];
Can i further break it into a character array like this one-
var x_character= ["h","e","l","l","o"];
if not, can you tell me how to know the character length of the x array..
Yes, use the .split() method:
var x = ["hello"]
x[0].split("")
Returns the array ["h","e","l","l","o"]. The "" argument means to split the string at each empty substring.
Create a new empty array, iterate through the original array and push each character into the new array. To get the length of the original array - use "x.length" (ie: "var arrayLength=x.length";)
var x = ["hello"];
var x_character=[];
for(i=0;i<x.length;i++)
{
var x_character.push(x[i])
}
This question already has answers here:
Javascript new Array and join() method
(5 answers)
Closed 6 years ago.
console.log(Array(4).join("hi"));
>> "hihihi"
I don't get what exactly is happening here?
join() is the opposite of split(). whereas split separates an array by the delimiting character you pass it, join will instead combine all the elements delimiting each one with whatever parameter you pass.
In this case the array is simply Array(4), so 4 undefined elements. combining these will yield "undefinedhiundefinedhiundefinedhiundefined".
Since js doesn't actually treat undefined as anything in this case, it turns it into an empty string and all you get is hihihi
edit: reference for my last statement from the join() documentation:
The string conversions of all array elements are joined into one string. If an element is undefined or null, it is converted to the empty string.
This question already has answers here:
Why is [1,2] + [3,4] = "1,23,4" in JavaScript?
(14 answers)
Closed 7 years ago.
I got this question from Interview,
[1,2] + [4,5,6][1]
JavaScript giving answer 1,25.
How it's happening? Please explain clearly.
Lets start with the last part and write it more verbose
var arr = [4,5,6];
var value = arr[1];
Is the same as
[4,5,6][1]
and as arrays are zero based, that gives 5, so we really have
[1,2] + 5;
The first part is equal to
[1,2].toString(),
and that returns the string "1,2" because the array is converted to a string when trying to use it in an expression like that, as arrays can't be added to numbers, and then we have
"1,2" + 5
Concatenating strings with numbers gives strings, and adding the strings together we get
"1,25"
It breaks down like this:
Starting with:
[1,2] + [4,5,6][1]
First, each side gets evaluated, and since the right-hand side is an array initializer and lookup, it comes out to 5:
[1,2] + 5
Now the + operator starts its work. It isn't defined for arrays, the first thing it does is try to convert its operands into either strings or numbers. In the case of an array, it'll be a string as though from Array#toString, which does Array#join, giving us:
"1,2" + 5
When you use + where either side is a string, the result is string concatenation.
First, [4,5,6][1] evaluates to the number 5
Then, the + operator is being applied to a first argument which is an Array and not a Number, so javascript assumes you're doing a string concatenation, not addition. Your array [1,2] becomes a string, which is "1,2". You can see this yourself with [1,2].toString().
The number 5 is now being appended to a string, so it to gets converted to a string, and appended together to get "1,25".