简体   繁体   中英

How can I iterate through an array in JS?

I am trying to write a function where I am iterating through the todos array. If todos has a value, return the value, else return a string saying "There are no To Dos yet!". But i am not sure if i wrote the function correctly or how i would invoke the function.

const todos = [
    {todo: 'Go to the gym', done: true},
    {todo: 'Cook dinner', done: false},
    {todo: 'Watch a movie', done: false}
];

todos.forEach(function(t) {
  if (todos.length === 0 ) {
    return 'There are no To Dos yet!';
  } else {
    return t.todo;
  }
});

Please try this:

const todos = [
    {todo: 'Go to the gym', done: true},
    {todo: 'Cook dinner', done: false},
    {todo: 'Watch a movie', done: false}
];

if (todos.length > 0) {
   todos.forEach(function(t) {
      console.log(t.todo);
   });
}
else {
   return 'There are no To Dos yet!';
}

You can test if every todo has been completed by using Array.prototype.every :

 const todos = [ { todo: 'Go to the gym', done: true }, { todo: 'Cook dinner', done: false }, { todo: 'Watch a movie', done: false } ]; printTodos(todos); todos.forEach(todo => todo.done = true); // All done.. printTodos(todos); function printTodos(todos) { return console.log(todos.every(todo => todo.done) ? 'There are no To-Dos yet!' : todos.filter(todo => !todo.done) .map(todo => '- ' + todo.todo) .join('\\n')) }


Simplified

Since we need to map them anyways, let's just filter them.

 const todos = [ { todo: 'Go to the gym', done: true }, { todo: 'Cook dinner', done: false }, { todo: 'Watch a movie', done: false } ]; printTodos(todos); todos.forEach(todo => todo.done = true); // All done.. printTodos(todos); function printTodos(todos) { let notCompleted = todos.filter(todo => !todo.done); return console.log(notCompleted.length === 0 ? 'There are no To-Dos yet!' : notCompleted.map(todo => '- ' + todo.todo).join('\\n')) }

Since forEach doesn't return anything, try .map() which returns a transformed array.

const getTodos = todos => {
  const result = todos.map(({ todo }) => todo);
  return result.length ? result : "There are no todos yet";
}

getTodos(todos) // ["Go to the gym", "Cook dinner", "etc."]
getTodos([]) // "There are no todos yet"

If you only want todos filtered by done property, chain filter to it:

const getTodos = todos => {
  const result = todos
    .filter(todo => !todo.done)
    .map(({ todo }) => todo)

  return result.length ? result : "There are no todos yet";
}

getTodos(todos) // ["Go to the gym"]
getTodos([]) // "There are no todos yet"

You could map the togo array and join it to a string or return the message.

 const getTodos = todos => todos.map(({ todo }) => todo).join(', ') || 'There are no To Dos yet!', todos = [{ todo: 'Go to the gym', done: true }, { todo: 'Cook dinner', done: false }, { todo: 'Watch a movie', done: false }]; console.log(getTodos([])); console.log(getTodos(todos));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM