Here's the scenario: I have an array named this.someArray.
this.someArray = ["Word", "123", "456"]
The content of this.someArray is generated dynamically, so the elements are not hardcoded.
My goal is to convert all numeric items into actual numbers (excluding words):
["Word", 123, 456]
To achieve this task, these are the steps I've come up with:
- Determine whether each element in the array is a word or number
To accomplish this, I've created a function:
isNumber(number) {
return !isNaN(parseFloat(number)) && !isNaN(number-0)
}
Use a forEach loop to check if each element is a word or number
this.someArray.forEach(element => { this.isNumber(element) });
Implement an if statement (if the element in this.someArray is a number, then remove the quotes)
However, I am unsure about the correctness of step 2 and uncertain about how to proceed with step 3.
Is there a way to achieve this?
Additional information:
This is what the dynamically generated array looks like:
https://i.sstatic.net/CgcFN.png
And here is the code responsible for generating the array:
this.someArray = this.biggerArray.map((n) => {
const data = [];
for (var key of Object.keys(n)) {
data.push(n[key].data);
}
return data;
});