Cam someone explain what this statement does? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have troubles understanding the meaning of (_,idx) in the following statement
arr.filter((_, idx) => idx % 2 === 0)
I understand it is filtering the array and just returning on the new array all elements that respect the condition(basically with even index). But i do not understand what this (_,idx) means?
Any help?

(_, idx) are two variable names.
arr.filter((currentValue, index, array) => )
As you can read here.
So "_" is currentValue and "idx" is the index.
"_" is not use but still defindes because it is a non-optional parameter, so you always have to give it an name.

Related

Can someone explain to me about array.forEach() [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
I have explore a few webs and watch some videos but I couldn't understand how array.forEach() works
This is the script that I found regarding that
let meals = ["rice", "meat", "salad"];
meals.forEach(capitalize);
meals.forEach(print);
function capitalize(element, index, array){
array[index] = element[0].toUpperCase() + element.substring(1);
}
function print(element){
console.log(element);
}
Can someone explain to me about how array[index] = element(0).toUpperCase() + element.substring(1); and meal.forEach(); works?
forEach() is an array function from JavaScript, that is used to iterate over items in a given array.
element[0] will return first character of each array element.
toUpperCase() is function use to convertthe string into upper case.
element.substring(1) this will return a string, except for the first character.

JS : Map an array ... So simple but BrainBreaker [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I face an issue with a so simple js transformation !
Here is my array
["one", "two"]
Here is what I want to output
["en/one", "en/one", "fr/two", "fr/two"]
What I'm trying to do with my knowledge
let a = ["one", "two"];
let b = a.map(x => `en/${x}`,`fr/${x}`);
console.log(b);
But I get an error:
ReferenceError: x is not defined
How can I make this work?
The way you wrote it, `fr/${x}` becomes a second argument to .map() (where x is not in scope). You need to combine the two values in an array...
And then you can use flatMap:
let b = a.flatMap(x => [`en/${x}`,`fr/${x}`]);

What is an example of a "custom function" and "recursion" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have been asked to show I have used these processes in my program but from looking up the definitions, I don't know what they mean. I am confident my program is complicated enough that it uses these processes, but I don't know exactly what they are. What would be an example of these processes used in javascript?
Unsure what you mean by custom function - but recursion is just a function that calls itself.
Example of recursion
countNumTimesToZero = (myNum, count = 0) => {
if (myNum - 1 === 0) return count + 1;
return countNumTimesToZero(myNum - 1, count + 1);
}

Remove an array-Javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array like this:
var animals=["cat","dog","snake","rhino"];
But sometimes I have to delete this array(remove it from the dom).
I have tried animals.remove; and $(animals).remove() and animals.remove() but none of them did the trick.Any ideas?
var animals=["cat","dog","snake","rhino"];
then to clear it do:
animals=[];
or
animals.length=0;
or
while(animals.length > 0) {
animals.pop();
}
Just assign the animals array to a value undefined and the array data will be dereferenced and garbage collected.
Donot try to call delete operator that is explicit removal.
animals = undefined
OR
animals = void 0
Thanks
Just Clear The Array
Using This 2 Methods
animals.length =0
animals=[];

Javascript declaration [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi this is a simple question. I was wondering is there any different when you declare something like this. Thanks
selectedData[key](val)
and
selectedData[key] = val
This line selectedData[key](val) is not a declaration, it's calling the function that is stored under the key key in the object selectedData and it's passing the parameter val to that function.
The other line selectedData[key] = val is assigning the value val to the key key in the object selectedData.
In the first case, you're calling whatever is in selectedData[key] as a function with val as an argument while in the second one you're assigning it.

Categories

Resources