1

If I have the following array

let initialdates = [2019-12-07,2019-12-08, NaN-NaN-NaN]

Why does the following not work for my process in removing the NaN....

let initialdates = [2019 - 12 - 07, 2019 - 12 - 08, NaN - NaN - NaN]
for (let [index, dateField] of initialdates.entries()) {
  if (isNaN(dateField)) {
    initialdates[dateField] = "found issue";
  }
}
console.log('the final array is: ' + initialdates);

It still prints out...

the final array is: 2019-12-07,2019-12-02,NaN-NaN-NaN

1
  • You're probably missing quotes in your array definition. 2019 - 12 - 07 is 2000 which is not a date. Commented Dec 7, 2019 at 17:42

4 Answers 4

2
initialdates[dateField] = "found issue";

should be

initialdates[index] = "found issue";
Sign up to request clarification or add additional context in comments.

Comments

1

To answer your question. You have made a mistake in your code. Instead of modifying value in an index, you're giving the value itself.

initialdates[index] = "found issue";

And to replace the function with ES6, you can use filter on it.

const initialdates = [2019 - 12 - 07,2019-12-8, NaN-NaN-NaN];
const result = initialdates.filter(value => !isNaN(value));

console.log('the final array is: ' + result);

which doesn't contain NaN.

2 Comments

just .filter(value=>value) and JS will handle that.
@VahidAlimohamadi probably he only wants to filter NaN values.
1

You need to use the index instead of the value to overwrite the member.

let initialdates = [2019 - 12 - 07, 2019 - 12 - 08, NaN - NaN - NaN]


for (let [index, dateField] of initialdates.entries()) {
  if (isNaN(dateField)) {
    initialdates[index] = "found issue"; // <-- use 'index'
  }
}
console.log('the final array is: ' + initialdates);

The way you had it, you were adding a property named "NaN" to the array.

Comments

1

let initialdates = [2019 - 12 - 07, 2019 - 12 - 08, NaN - NaN - NaN]
initialdates = initialdates.filter(i=>i);
console.log('the final array is: ' + initialdates);

If there is minus, yes a simple filter will remove NaN. But if they are dashes to separate date parts, you need to make them string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.