What is the difference between those two join usage? [duplicate] - javascript

This question already has an answer here:
join method not working on array JAVASCRIPT
(1 answer)
Closed 4 years ago.
Could anyone simply explain why the first method doesnt work as expected but inside console.log works perfect?
Simply I expected that It needs to return string but returns array in the first console
var elements = ['Fire', 'Wind', 'Rain'];
elements.join(''); //why that one is not working?
console.log(elements);
// expected output: Fire,Wind,Rain
console.log(elements.join(''));
// expected output: FireWindRain

You need to assign elements.join(''); to a variable:
var elements = ['Fire', 'Wind', 'Rain'];
elements = elements.join('');
console.log(elements);

Related

Filter Out Element from Nested Array [duplicate]

This question already has answers here:
Remove all falsy values from an array
(26 answers)
How to remove false values from array?
(6 answers)
Closed 3 days ago.
let input array=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]
let output array=[["1.81","2.24"],["5.62","6.26"],["2.31","1.64"]];
I have a nested input array which contains smaller arrays and false statement as shown in the console. How do I remove the false statement from the input array? I have tried using for loop to loop through all the 6 elements to check each element with a (if !==false), then push into a new array called the output array but I could not get it to work? May I know how to solve this? Your help will be very much appreciated :)
Directly use Array#filter:
let input=[["1.81","2.24"],["5.62","6.26"],false,["2.31","1.64"],false,false]
let res = input.filter(Boolean);
console.log(res);

How to get the one before last element of an array in JavaScript? [duplicate]

This question already has answers here:
get the second to last item of an array?
(11 answers)
Closed 10 months ago.
I wanna to get one before last element of array in JavaScript. but in console, when I use this code "array.length -2"dose not work, and it just return me the length of array minus 2.
and when I use this code "array.at[-2]" it returns "undefined"!
const arr = [1,2,3,4,5,6]
console.log(arr[arr.length -2])
//5

What does `empty` mean in a JS array? [duplicate]

This question already has answers here:
What is "undefined x 1" in JavaScript?
(5 answers)
What's the difference between empty items in a JavaScript array and undefined? [duplicate]
(2 answers)
Closed 3 years ago.
Can anyone help me understand what an empty means in a JS array?
I just noticed an array in chrome devtools with an empty value. I created the one below, and by putting two commas together I can produce an empty (see below). Granted, I'm relatively new to JS, but since empty isn't a JS primitive I would have expected either:
a) a null or undefine3d value for a[3]
b) a.length to be 4
What does empty mean?
Array literals can include elisions, e.g.
[0,1,,3];
The two commas together mean "there is no element at index 2". Many stringified versions will show undefined at index 2, however that is misleading as there is no element at all at that position.
E.g.
let arr = [0,1,,3];
arr.forEach((v, i) => console.log(`index ${i} has value ${v}`));
console.log('stringified: ' + arr);
console.log('SO console:', arr);
console.log('JSON: ' + JSON.stringify(arr));
An alternative is to show "empty", which (IMHO) isn't really suitable either, it should be shown as in the literal (i.e. as for "stringified").

array splice and console.log [duplicate]

This question already has an answer here:
Array is showing up as empty, yet it contains values [duplicate]
(1 answer)
Closed 5 years ago.
When I perform operations on an array in javascript, console.log shows the array as having been already changed BEFORE I do an operation on the array. I can reproduce this in chrome and firefox. Anyone know why this is?
var myTabs = [[0,0,0],
[0,0,0],
[0,0,0],
[0,0,0]];
console.log(myTabs);
myTabs[0].splice(1, 1);
console.log(myTabs);
See this for code:
https://jsfiddle.net/mxrh33t0/1/
When you expand a logged object in Chrome, you are expanding the last reference to it, not a copy of that object at the moment that it was logged.
In your case, the last reference was to the array after the splice() method had been called. If you would like to verify the difference, you will have to be more specific with your logging:
var myTabs = [[0,0,0],
[0,0,0],
[0,0,0],
[0,0,0]];
console.log(myTabs[0].join(","));
myTabs[0].splice(1, 1);
console.log(myTabs[0].join(","));
You can expand on that if you really want to see more.

.find('.foo').text() as array [duplicate]

This question already has answers here:
Get array of text values using selector instead of iterating?
(7 answers)
Closed 9 years ago.
I have html with multiple things classed .text. I the perform
console.log($(whatever).find('.foo').text());
The result is a textstring with all the contents in it. Can I save it to an array instead somehow?
You can use the map method to get this done
var textArray = $(whatever).find('.foo').map(function() {
return $(this).text();
}).get();
This will get you an array.. If you want to display it as a string then just use join along with it.
Check Fiddle
var arr = [];
$(whatever).find('.foo').each(function() {
arr.push($(this).text());
});

Categories

Resources