How to print a String array in javascript? - javascript

String array :
var name=new String('Here','Is','Wasif');
I have tried this to print it :
for(var i=0;i< name.length;i++)
document.write(name[i]);

You don't have a string array.
Try
var name=['Here','Is','Wasif'];
for(var i=0;i< name.length;i++)
document.write(name[i]);

You can check this code
var arr = name=['Here','Is','Wasif'];
for(var i=0;i<arr.length;i++){
document.write(arr[i]);
}
You can try your code here :
http://jsfiddle.net/4Sgh5/

You could also use array join. A bit more simple that a for loop.
var arr = ['Here', 'Is', 'Wasif'];
document.write(arr.join(' '));

Related

How to remove substring and comma if exist on left or right side

I have the following string:
"house,car,table"
I need to properly handle the comma removal, so much so that If I remove "car" the output should be:
"house,table"
If I remove "table" the output should be:
"house,car"
You can use the .split() in an array then .filter() out the target text after words .join() to create a string.
var str = "house,car,table";
str = str.split(',').filter(x => x !== 'car').join(',');
console.log(str)
You can use string#split, string#indexOf array#splice and arr#join
var str = "house,car,table";
var arr = str.split(',');
var index = arr.indexOf('car');
arr.splice(index, 1);
console.log(arr.join(','));
There are several ways. #Satpal has offered a way that is optimized. but another way:
var array ="house,car,table";
var arraySplit = array.split(",");
var newArray = [];
for (i=0; i<arraySplit.length; i++)
{
if (arraySplit[i] != "car")
{
newArray.push(arraySplit[i]);
}
}
var joinedArray = newArray.join(",");
console.log(joinedArray);
function format(name){
var nameStr="house,car,table";
if(nameStr.indexOf(name)==-1)return -1;
nameStr+=",";
nameStr=nameStr.replace(name+",","");
return nameStr.substring(0,nameStr.length-1);
}
console.log(format("house"));
console.log(format("table"));

how to split this complex json array?

I have an array like
["1-India","2-xxx","3-yyyy"]
I want array like
["India","xxxx"]
Please help me to get this functionality.
You can use combination of .map() and .split().
var newArr = ["1-India","2-xxx","3-yyyy"].map(x=> x.split('-')[1]);
console.log(newArr)
As I said in my comment. That what you have there, is not an JSON, it's an array. I think this could be an easy way to achieve what you want:
var arr = ["1-India", "2-xxx", "3-yyyy"];
var newArray = [];
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].replace(/[0-9-]/g, '');
newArray.push(arr[i]);
}
console.log(newArray)

concat empty / single item array

Ok, so I have this simple code :
for(var i=0; i<lines.length; i++) {
elements += myFunction(lines[i]);
}
Where elements is an empty array at the start and myFunction() is just a function that returns an array of strings.
The problem is that if myFunction() returns an array with a single string, the += is interpreted as a string concat in place of an array concat.
At the end of the loop the result is just a long string and not an array.
I tried push()ing the values in place of concatenation, but this just gives me a two dimensional matrix with single item arrays.
How can I solve this typecasting problem ? Thank you in advance !
Try :
for(var i=0; i<lines.length; i++) {
elements [i] = myFunction(lines[i]);
}
I suppose it solves the problem.
You can use the Array concat function:
elements = elements.concat(myFunction(lines[i]));
Presumably you want something like:
var arrs = [[0],[1,2],[3,4,5],[6]];
var result = [];
for (var i=0, iLen=arrs.length; i<iLen; i++) {
result = result.concat(arrs[i]);
}
alert(result); // 0,1,2,3,4,5,6
Ah, you want to concatenate the results of a function. Same concept, see other answers.
You can also use myArray[myArray.length] = someValue;
let newArray = [].concat(singleElementOrArray)

What's wrong with this code? I try to create multiple arrays in a loop

I try to create multiple arrays in a loop. I was told that the correct way to do this is by creating 2-dimentional arrays. So I made the following code, but it keep telling me eleArray[0] is undefined. anyone? Thanks
var eleArray = [];
for(var i=0;i<rssArray;i++)
{
eleArray[i] = [];
}
eleArray[0][0] = "tester";
alert(eleArray[0][0]);
Assuming that rssArray is an array as the name implies, you need to loop based on the length:
for(var i=0;i<rssArray.length;i++)
Is the rssArray variable being initialized correctly?
Here's a working example of your question.
jsfiddle
var array = [], length = 10, i;
for(i = 0; i < length; i++){
array[i] = [];
}
array[0][0] = "Hello, World!";
document.getElementById("output").innerHTML = array[0][0];

How to append (connect) strings to construct a new string?

I have a array of strings:
str[1]='apple';
str[2]='orange';
str[3]='banana';
//...many of these items
Then, I would like to construct a string variable which looks like var mystr='apple,orange,banana,...', I tried the following way:
var mystr='';
for(var i=0; i<str.length; i++){
mystr=mystr+","+str[i];
}
Which is of course not what I want, is there any efficient way to connect all this str[i] with comma?
just use the built-in join function.
str.join(',');
Check out join function
var str = [];
str[0]='apple';
str[1]='orange';
str[2]='banana';
console.log(str.join(','));
would output:
apple,orange,banana
The fastest and recommended way of doing this is with array methods:
var str = [];
str[1] = 'apple';
str[2] = 'orange';
str[3] = 'banana';
var myNewString = str.join(',');
There have been various performance tests showing that for building strings, using the array join method is far more performant than using normal string concatenation.
You need this
var mystr = str.join(',');
how about 'join()'?
e.g.
var newstr = str.join();
You're looking for array.join i believe.
alert(['apple','orange','pear'].join(','));
Is this what you want?
var str = new Array(); //changed from new Array to make Eli happier
str[1]='apple';
str[2]='orange';
str[3]='banana';
var mystr=str[1];
for(var i=2; i<str.length; i++){
mystr=mystr+","+str[i];
}
console.log(mystr);
would produce
apple,orange,banana

Categories

Resources