Skip to main content

Overview


Start with this array of objects:

const tweets = [
{
id: 10512,
message: "Hello Twitter 👋",
stats: {
likes: 41,
retweets: 54
}
},
{
id: 41241,
message: "How do you keep track of your notes?",
stats: {
likes: 14,
retweets: 20
}
}
];

Array .filter()​


tweets.filter(tweet => {
console.log(tweet); // visualize tweet
return tweet.stats.likes > 30;
})

// rewritten with an arrow function with implicit return
tweets.filter(tweet => tweet.stats.likes > 30);

Array .find()​


Calling the .find() method on an array of objects will return the first object that matches the condition you specify in the callback or undefined if no objects satisfy the condition.

const searchId = 41241;
const tweet = tweets.find(tweet => tweet.id === searchId);
console.log(tweet); // {...} (2nd tweet object)

Array .some()​


Calling the .some() method on an array of objects will return true when at least one item in the array satisfies the condition you specified in the callback. Otherwise, it returns false.

tweets.some(tweet => tweet.stats.likes > 30); // true (at least one has more than 30 likes)
tweets.some(tweet => tweet.stats.likes > 100); // false (none of the tweets satisfy this condition)

Array .every()​


Calling the .every() method on an array of objects will return true when every item in the array satisfies the condition you specified in the callback. Otherwise, it returns false.

tweets.every(tweet => tweet.status.likes > 10); // true (all the tweets have more than 10 likes)
tweets.every(tweet => tweet.status.likes > 30); // false (some tweets don't have more than 30 likes)

Quick recap:


  • Calling the .filter() method on an array of objects will return an array containing the objects that pass the condition you specify in the callback.
  • Calling the .find() method on an array of objects will return the first object that matches the condition you specify in the callback, or undefined if no objects satisfy the condition.
  • Calling the .some() method on an array of objects will return true when at least one item in the array satisfies the condition you specified in the callback. Otherwise, it returns false.
  • Calling the .every() method on an array of objects will return true when every item in the array satisfies the condition you specified in the callback. Otherwise, it returns false.

Try it:


Complete the getCompletedCourses function such that it returns the courses (could be 0 or more) that are completed. You can tell if a course is completed or not by checking the isCompleted property.

const getCompletedCourses = courses => {
return courses.filter(course => course.isCompleted === true); //return the courses filtered where isCompletes is equal to true.
}

// Sample usage - do not modify
const sampleCourses = [{
id: 1,
name: "Learn Programming",
isCompleted: true
}, {
id: 2,
name: "Learn JavaScript",
isCompleted: false
}];
console.log(getCompletedCourses(sampleCourses));

Complete the getBigGroups function such that it returns the chats that have 100 messages or more.

const getBigGroups = groups => {
return groups.filter(group => group.details.messageCount >= 100)
}

// Sample usage - do not modify
const sampleGroups = [{
id: 1,
title: "Football",
details: {
messageCount: 30,
public: true
}
}, {
id: 2,
title: "Family",
details: {
messageCount: 1014,
public: false
}
}];
console.log(getBigGroups(sampleGroups));

Next one​


Complete the getFamilyGroup function such that it returns the first and only group that has the title "Family".

For the purpose of the challenge, you can assume that the groups array of object will always contain one (and only one) group with the title "Family"

const getFamilyGroup = groups => {
return groups.find(group => group.title === "Family"); //filter returns them all, find returns the one.
}

// Sample usage - do not modify
const sampleGroups = [{
id: 1,
title: "Football",
details: {
messageCount: 30,
public: true
}
}, {
id: 2,
title: "Family",
details: {
messageCount: 1014,
public: false
}
}];
console.log(getFamilyGroup(sampleGroups));

This one is gonna use some() or every()​


Complete the allGroupsPublic function such that it returns true if all the groups are public (based on isPublic), and false otherwise.

const allGroupsPublic = groups => {
return groups.every(group => group.details.isPublic === true); //return if every group.details.isPublic is true ;)

}

// Sample usage - do not modify
const sampleGroups = [{
id: 1,
title: "Football",
details: {
messageCount: 30,
isPublic: true
}
}, {
id: 2,
title: "Family",
details: {
messageCount: 1014,
isPublic: false
}
}];
console.log(allGroupsPublic(sampleGroups));

Additional use cases


Taking this variable here:

const tweets = [
{
id: 10512,
message: "Hello Twitter 👋",
stats: {
likes: 41,
retweets: 54
}
},
{
id: 41241,
message: "How do you keep track of your notes?",
stats: {
likes: 14,
retweets: 20
}
}
];

We can use the .map() and .join() methods to convert an array of objects into a CSV string. The .map() allows you to specify which property (or properties) you'd like to extract from the objects. Then, you can call .join() on the array to glue them together.

  • .map(): extract
  • .join(): glue
//for instance...
const csv = tweets.map(tweet => tweet.message).join(", ");
console.log(csv); // "Hello Twitter 👋, How do you keep track of your notes?"

Object Destructuring: When accessing nested properties, you may find it useful to use object destructuring:

tweets.forEach(tweet => {
const {likes, retweets} = tweet.stats; //takes the likes and retweets inside of the stats and puts them into likes and retweets variables.
console.log(likes, retweets);
});

Export some users that are verified.


Complete the exportVerifiedUsers function such that it returns a CSV string of the names of the users that are verified.

const exportVerifiedUsers = users => {
let verifiedArray = users.filter(user => user.isVerified === true)
console.log(verifiedArray.map(user => user.name).join(", "))
return verifiedArray.map(user => user.name).join(", ")
}

// Sample usage - do not modify
const sampleUsers = [{
name: "Sam",
isVerified: true
}, {
name: "Alex",
isVerified: false
}, {
name: "Charlie",
isVerified: true
}];
console.log(exportVerifiedUsers(sampleUsers)); // "Sam, Charlie"

Chapter Recap


  • Calling the .filter() method on an array of objects will return an array containing the objects that pass the condition you specify in the callback.
  • Calling the .find() method on an array of objects will return the first object that matches the condition you specify in the callback or undefined if no objects satisfy the condition.
  • Calling the .some() method on an array of objects will return true when at least one item in the array satisfies the condition you specified in the callback. Otherwise, it returns false.
  • Calling the .every() method on an array of objects will return true when every item in the array satisfies the condition you specified in the callback. Otherwise, it returns false.