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)
  • 相关阅读:
    YYModel Summary
    Custom-->TableView_Swizzle
    创建UIBarButtonItem的分类
    为家庭版系统添加组策略
    JSDOM
    JavaScript
    python基础三元表达式和内置函数列表
    二.ubuntu14.04 3D特效设置
    一.ubuntu14.04安装、亮度设置、显卡设置等一体化讲解
    oracle12c及PLSQL Developer安装全程记录
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5097692.html
Copyright © 2011-2022 走看看