Can someone explain to me about array.forEach() [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 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.

Related

Cam someone explain what this statement does? [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 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.

Remove non-numeric items in a listu using regex [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 3 years ago.
Improve this question
I am working with an array in Javascript that contains several IDs in them but I would like to filter out all non-numeric entries using regex and return that array of just numbers. For example, I have myArray = ['131125150138677','CI%20UW%20SYSTEMS%20S','040964100010832'] where I want to get rid of the second item in the list since it's non-numeric.
So use Filter and test to see if they are numbers
var myArray = ['131125150138677', 'CI%20UW%20SYSTEMS%20S', '040964100010832']
var filtered = myArray.filter(Number)
console.log(filtered)
var filtered2 = myArray.filter(s => s.match(/^\d+$/))
console.log(filtered2)

Javascript convert [[ ]] to [] [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 3 years ago.
Improve this question
Convert
var a = [['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']]
to
var a= ['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']
I dont want two square brackets in front and end.
I want output
a[2] = 'a123'
The best way to do is just assigning the first value to the array:
var a = [['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']];
a = a[0];
console.log(a);
But the right way of dealing it should be making sure that the endpoint or whatever that outputs should output correctly.
You can use Array.prototype.flat().
console.log([['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']].flat());
This solution works if it's extremely nested too:
console.log([
['12ae11ee12-1bhb222'],
['2019-10-10T19:46.19.632z', 'a123']
].flat());

Need to replace numbers between [...] with Javascript or jQuery [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 6 years ago.
Improve this question
Inside a string: I need to replace the number between [ ] by another number with Javascript or jQuery.
What is the best way to proceed ?
Eamples
"[12345].Checked" > "[1].Checked"
"[123].RequestID" > "[21].RequestID"
Thanks for your help.
function fixNum(str, newnum) {
return str.replace(/\[\d+\]/,'[' + newnum + ']')
}
Use .replace:
"[12345].Checked".replace(/\[(\d+)\]/, "[1]") // "[1].Checked"
With regular expressions:
/\[(\d+)\]/
It searches a number (no matter length) inside a [] set.
Combined with replace you can make it.
Test it:
https://regex101.com/r/zT5kD0/1
Use replace method to replace the part
function replaceStr(string,orgText,replaceText){
var res = string.replace(orgText, replaceText);
return res;
}
console.log(replaceStr("[12345].Checked",'12345','1'));
jsfiddle

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=[];

Categories

Resources