to understand the split method I went over this link https://www.w3schools.com/jsref/jsref_split.asp
but not sure why comma not adding after 3 and why empty array not showing up in the output
is it just doing array concatenation
i debugged but not sure
can you guys let me know.
[123] + [] + 'foo'.split('');
"123f,o,o"
When the array is converted to string. Implicitly join() is called on it. So [].join() is '' that's why it doesn't show up in string.
But if you use some empty elements then it will show ,
console.log([123] + [,] + 'foo'.split(''));
How to concat arrays:
There can be different ways to concat two or more arrays. The modern one is using Spread Operator.
console.log([...[123], ...[],...'foo'.split('')]);
Related
I have a string value as abc:language-letters-alphs/EnglishData:7844val: . I want to extract the part language-letters-alphs/EnglishData, the value between first : and second :. Is there a way to do it without storing each substrings on different vars? I want to do it the ES6 way.
You can do this two ways easily. You can choose what suits you best.
Using String#split
Use split method to get your desired text.
The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method's call.
let str = 'abc:language-letters-alphs/EnglishData:7844val:'.split(':')
console.log(str[1]) //language-letters-alphs/EnglishData
Using String#slice
You can use [ Method but in that you have define the exact indexes of the words you want to extract.
The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.
let str = 'abc:language-letters-alphs/EnglishData:7844val:'
console.log(str.slice(4, 38)) //language-letters-alphs/EnglishData
const str = "abc:language-letters-alphs/EnglishData:7844val:"
const relevantPart = str.split(':')[1]
console.log("abc:language-letters-alphs/EnglishData:7844val:".split(":")[1])
I have an object with strings in it.
filteredStrings = {search:'1234', select:'1245'}
I want to return
'124'
I know that I can turn it into an array and then loop through each value and test if that value in inside of the other string, but I'm looking for an easier way to do this. Preferably with Lodash.
I've found _.intersection(Array,Array) but this only works with Arrays.
https://lodash.com/docs#intersection
I want to be able to do this without having to convert the object to an array and then loop through each value because this is going to be potentially holding a lot of information and I want it to work as quickly as possible.
Thank you for you help.
Convert one of the strings (search) to a RegExp character set. Use the RegExp with String#match on the other string (select).
Note: Unlike lodash's intersection, the result characters are not unique, so for example 4 can appear twice.
var filteredStrings = {search:'1234', select:'124561234'}
var result = (filteredStrings.select.match(new RegExp('[' + filteredStrings.search + ']', 'g')) || []).join('');
console.log(result);
I'm new to javascript and currently working my way through code school. I've done this exercise before and have no issue with it, but after not doing anything for a week i decided to go back and redo some of the exercises... now i'm stuck on this one :( here is the instructions and below that is my code... what I'm I doing wrong code school tells me I'm not adding white space between the two words?
Now alert to the screen the entire first movie in eightiesMovies, but only using the >eightiesMovies variable. For now, use the concatenation operator to unite the words into one >string. Remember to be attentive to necessary whitespace…
var movie1 = [ 16, "Candles"];
var movie2 = [ 3, "Men", "and", "a", "Baby"];
var eightiesMovies = [ movie1, movie2];
my code
alert(eightiesMovies[0,1] + " " + eightiesMovies[0,1]);
To access an array in an array, you use two sets of brackets after each other:
alert(eightiesMovies[0][0] + " " + eightiesMovies[0][1]);
(Also you were using the same item twice instead of two items.)
What's happening in your original code is that you are accidentally using the comma operator, that's why you don't just get a syntax error for that code. An expression like 0,1 will evaluate both 0 and 1, and then the value of the expression is the last value. That will make the code access eightiesMovies[1] which is an array, and the string concatenation would convert the array to a string. The result is "3,Men,and,a,Baby 3,Men,and,a,Baby" rather than the "16 Candles" that is expected.
To join the elements of an array to form a string use join()
in your example
eightiesMovies[0].join(" ");
I have a variable which contains the values like this ..
["09:09:49", "00:14:09", "00:05:50", "02:38:02", "01:39:28"]
Now as per my need i have to formate like this ..
[09:09:49, 00:14:09, 00:05:50, 02:38:02, 01:39:28]
for this i tried
callduration=[];
callduration=["09:09:49", "00:14:09", "00:05:50", "02:38:02", "01:39:28"];
var newstring = callduration.replace(/\"/g,'');
But it is giving error ..
TypeError: callduration.replace is not a function
var newstr=callduration.replace(/\"/g,'');
Please help me.
Thanks in advance..
First off, you must note that callduration is an array. Arrays do not have a replace method, hence the error.
As mentioned by #Felix Kling, the quotes are just string delimiters. They are not part of the string values contained in your array of strings. For example, when accessing callduration[0] you will get a string containing the 09:09:49 sequence of characters.
However, if you really need a string in the requested format, here it is:
var callduration = ["09:09:49", "00:14:09", "00:05:50", "02:38:02", "01:39:28"];
var newstr = '[' + callduration.join(', ') + ']';
newstr; //"[09:09:49, 00:14:09, 00:05:50, 02:38:02, 01:39:28]"
Though this probably won't be of much use unless you have some very specific use case in mind.
callduration is an array. That means it contains a sequential, ordered list of items. Those items must be something that can exisdt in javascript. As your array exists like this:
["09:09:49", "00:14:09", "00:05:50", "02:38:02", "01:39:28"]
it is an array of strings. Each time value is represented by its own string. The quote marks are not actually part of the string - that' just how a string is represented when typing it.
If you want the array to be an array of something other than strings, you would need to specify what data type you want it to be. 09:09:49 as you've asked, it not a legal javascript piece of data.
Some choices that you could use:
An array of numbers where each number represents a time value (say milliseconds since midnight).
An array of Date objects.
If you have an array of strings now and you wanted to convert it to either of the above, you would loop through your existing array, parse the string you have now into an actual numeric time and then convert that into whatever numeric or object format you want to be in the array.
i would like to convert this string:
'[
['Row1 of first array', 'Row2 of first array'],
['Row1 of 2nd array', 'Row2 of 2nd array']
]'
Into an array with three arrays of one dimension and two items.
My expected output is an array with 2 elements:
Array 1
Array 2
And every array has two elements inside.
Is there any in Jquery to do this conversion?
That's not a valid string -- you're nesting single quotes inside of single quotes. However, if you convert the string into one using double quotes on the inside:
str = '[ ["Row1 of first array", "Row2 of first array"], ["Row1 of 2nd array", "Row2 of 2nd array"] ]'
then you could simply parse it as a JSON object:
arr = $.parseJSON(str); // returns a two-dimensional array
This is far, FAR safer than using eval, which should only be done when you know EXACTLY what's inside the string, and even then, it's a sign of a lazy developer. When using parseJSON, you know that you're getting either an object or an array when you're done -- when using eval, anything might happen.
I guess eval will work:
var str = eval("[['Row1 of first array', 'Row2 of first array'],['Row1 of 2nd array', 'Row2 of 2nd array']]");
console.log(str);