Can i further break an array in javascript [duplicate] - javascript

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

Related

Sorting strings in javascript [duplicate]

This question already has answers here:
How do you reverse a string in-place in JavaScript?
(57 answers)
Closed 3 years ago.
I would like to sort strings in javascript containing comma separated values in different e.g.
var Str = "8,0,2,10"
I want to sort it like below example form the last one to first one:
var NewStr = "10,2,0,8"
You can convert string to array using split() and reverse the array element using reverse() and then convert result to string again using join() like this:
var Str = '8,0,2,10';
var dif = Str.split(',').reverse().join(',');
console.log(dif);

How to get a Substring JAVASCRIPT? [duplicate]

This question already has answers here:
Get Substring between two characters using javascript
(24 answers)
Closed 4 years ago.
I need to iterate over strings that are inside an array, to get a sub-string in each string.
Substrings are between "()"
Something like this..
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
//return
//'cat','red','apple'
How I could do that?
You can do this using substring and lastIndexOf functions.
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
myArray.forEach(function(e){
console.log(e.substring(e.lastIndexOf("(") + 1, e.lastIndexOf(")")))
})

Javascript two dimensional string array update value [duplicate]

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.

get value after last slash (/) [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 7 years ago.
I can get background image by doing
$('#item').css('background-image').
but the result is url('www.example.com/something/123.png')
How can get get only the 123 value? I can't use string position because the value of the img name doesn't' necessary is 3 characters.
You can use lastIndexOf and substring:
var uri= $('#item').css('background-image');
var lastslashindex = uri.lastIndexOf('/');
var result= uri.substring(lastslashindex + 1).replace(".png","");
or using .split() and .pop()
var uri= $('#item').css('background-image');
var result=uri.split("/").pop().replace(".png","");

How to convert string separated by commas to array? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert JS object to JSON string
Store comma separate values into array
I have a string containing values separated with commas:
"1,4,5,11,58,96"
How could I turn it into an object? I need something like this
["1","4","5","11","58","96"]
This will convert it into an array (which is the JSON representation you specified):
var array = myString.split(',');
If you need the string version:
var string = JSON.stringify(array);
In JSON, numbers don't need double quotes, so you could just append [ and ] to either end of the string, resulting in the string "[1,4,5,11,58,96]" and you will have a JSON Array of numbers.
make it an array
var array = myString.split(',');

Categories