Skip to content

Instantly share code, notes, and snippets.

@MaraAlexa
Created May 22, 2017 13:35
Show Gist options
  • Save MaraAlexa/7264e3abc40b1027badffef96b99309d to your computer and use it in GitHub Desktop.
Save MaraAlexa/7264e3abc40b1027badffef96b99309d to your computer and use it in GitHub Desktop.
Array.prototype.some();
// Array.prototype.some()
var tasks = [
{
title: 'Do laundry',
completed: true
},
{
title: 'Feed the cat',
completed: true
},
{
title: 'Watch the array lessons on egghead.io',
completed: true
}
];
var list = document.querySelector('.task-list'); // select the <ul> element
list.classList.add(
tasks.some(task => task.completed === false) // if any task has the completed value false => return true => add class uncompleted
? 'task-list--uncompleted'
: 'task-list--completed' // else if task.completed === false => returns false => add class 'completed'
);
list.innerHTML = tasks
.map(x => x.completed ? `<s>${x.title}</s>` : x.title)
.map(x => `<li>${x}</li>`)
.join('');
// Array.prototype.some() is used when you just want a quick query on the array when you just want a YEs or No answer.
// Returns true or false
// Basic example
var items = [1, 2, 3, 4, 5];
var hasThree = items.some(item => (item === 3)); // as soon as the wanted item is found, the loop stops and returns true
console.log(hasThree); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment