We can use the destructing and rest parameters at the same time when dealing with Array opration.
Example 1:
let [first, ...remainingUsers] = ["Sam", "Tyler", "Brook"]; addActiveUsers(first, remainingUsers); // "Sam", ["Tyler", "Brook"]
Example 2:
function buildTopicInfo(topic){ let title = `<h1>${topic.title}</h1>`; let author = `<small>${topic.author}<small>`; return [title, author]; } let topic = getCurrentTopic(); let [topicTitle, topicAuthor] = buildTopicInfo(topic);
Example 4:
let topicId = currentTopic(); let activeUsers = ["Sam", "Tyler", "Brook"]; for( let user of activeUsers ){ notifyTopicReply(topicId, user); }
- for...of can only apply on the intereable object, like array, but not object
Example 5:
This code will report an type error, because for ... of can not be used for object.
let topicInfo = { title: "New Features in JS", replies: 19, lastReplyFrom: "Tyler" }; for(let [k, v] of topicInfo){ console.log(`${k} - ${v}`); }
Example 6: Array.find()
let recentTopics = [ { title: "Semi-colons: Good or Bad?", isLocked: true }, { title: "New JavaScript Framework Released", isLocked: true }, { title: "ES2015 - The Shape of JavaScript to Come", isLocked: false } ]; recentTopics.find( (topic)=> !topic.isLocked)