I am trying to understand the filter method in JavaScript.
I have an array and with a checkbox and a global variable I want to control what to show in the array.
If the checkbox is unchecked and the value of the variable filterTodos is false (false is default), I want to show all the items of the array. If the checkbox is checked and the variable value of variable filterTodos is true, I want to only show the tasks that are not complete. (completed: false)
I know that my code for the filter method is wrong as it's not doing anything, and when the checkbox is unchecked it displays the array multiple times.
This is my code:
let todos = [{
text: 'Order airline tickets',
completed: false
}, {
text: 'Vaccine appointment',
completed: true
}, {
text: 'Order Visa',
completed: true
}, {
text: 'Book hotell',
completed: false
}, {
text: 'Book taxi to airport',
completed: true
}]
let filterTodos = false
showTodos(todos);
function addTodo(add_todo) {
let p = document.createElement('p')
p.textContent = add_todo.text
document.querySelector('body').appendChild(p)
let checkBox = document.createElement("input")
let label = document.createElement("label")
checkBox.type = "checkbox"
checkBox.value = addTodo.completed
document.querySelector('body').appendChild(checkBox)
//create variable to generate random id for radiobutton
const id = Math.random().toString(36)
checkBox.setAttribute("name", id)
//Switch loop over the completed
switch (add_todo.completed) {
case true:
checkBox.setAttribute("checked", "true") // Mark true button as checked
break
case false:
checkBox.setAttribute("unchecked", "false") // Mark false button as checked
break
default:
break
}
document.querySelector('body').appendChild(label)
label.appendChild(document.createTextNode(add_todo.completed))
}
function showTodos(show_todos) {
//function to show the whole object
show_todos.forEach(function(todo) {
addTodo(todo);
})
}
function unComplete(uncompleteTodos) {
unComplete.filter(!todo.completed)
}
function hideCompleted() {
document.querySelector("#filterTodo").addEventListener('change', function(e) {
if (document.getElementById('filterTodo').checked) {
filterTodos = true;
console.log(filterTodos);
filterTodos = todos.filter(unComplete)
unComplete(todos)
} else {
filterTodos = false;
console.log(filterTodos);
showTodos(todos)
}
})
}
<h1>Todos</h1>
<label style="font: bold 1.5em courier !important">
<input type="checkbox" id="filterTodo" onChange="hideCompleted()">Hide completed
</label><br><br>
<input id="search-todo" type="text" placeholder="Search todo">
<button id="reset-search" type="reset" value="reset" onclick="window.location.reload()">New search</button>
<div id="todos"></div>
<form action="" id="addTodo">
<input type="text" name="inputTodo" placeholder="Insert new todo">
<button>Add Todo</button>
</form>
<script src="js/index2.js"></script>
hideCompleted()will be called it will callshowTodos()and then it will calladdTodo()and hence it will add new items means items will be doubleonChangeattribute on the checkbox that calls a function that sets anotherchangehandler that does the same thing. Secondly, you usefilter()properly in one part of your code, but then try to run aforEach()loop on something that doesnt' exist - that shows up as an error in the console. You can start by fixing those first.function unComplete(uncompleteTodos) { unComplete.filter(!todo.completed) }also not works. You cannot filter the function