[ES6] Array -- Destructuring and Rest Parameters && for ..of && Arrat.find()

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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值