zoukankan      html  css  js  c++  java
  • [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)
  • 相关阅读:
    find命令
    shell编程基础
    grep命令
    awk命令
    结对项目之需求分析与原型模型设计
    使用Git进行代码管理的心得
    软件工程的实践项目的自我目标
    第五次作业——团队项目——需求规格说明书
    调研android开发环境的发展演变
    结对编程总结
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5097692.html
Copyright © 2011-2022 走看看