Javascript two dimensional string array update value [duplicate] - javascript

This question already has answers here:
Why can't I swap characters in a javascript string?
(6 answers)
Closed 7 years ago.
I have the following code:
var data = ["z wwwww ","www w ","w b w ww","w w p w","w w w","wwbwp w"," wy www"," wwwww "];
console.log(data[0][0]); // outputs "z"
data[0][0]="x";
console.log(data[0][0]); // still output "z". Shouldn't it show "x"?
What am I missing here?

A two dimensional array is an array, that includes elements that are array's themselves. The example you provided is not a 2D array.
The element in question is in fact a String.
data[0] - Gives you the first element in your data array, which is a string.
data[0][0] - Gives you the first character of this string element.
In JavaScript, a string is a collection of characters, but it isn't an array itself. It can be transformed into a string with string.split('').
Anyways, the reason it shows z instead of x, is because strings are immutable. That means their values can not change. Instead, new objects are created.

Related

Compare two strings in JS to see if the first big string contains exact same match or not [duplicate]

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.

Convert array of strings to array of numbers [duplicate]

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);

Can i further break an array in javascript [duplicate]

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])
}

What happens when we declare Array(4) in JavaScript? [duplicate]

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.

How [1,2] + [4,5,6][1] = 1,25 in JavaScript [duplicate]

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".

Categories