當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Embeer.js Ember.NativeArray lastIndexOf()用法及代碼示例


Ember.js 是一個開源 JavaScript 框架,用於開發基於 Model-View-Controller (MVC) 架構的大型客戶端 Web 應用程序。 Ember.js是使用最廣泛的前端應用框架之一。它的目的是加速開發並提高生產力。目前,它被大量網站使用,包括 Square、Discourse、Groupon、Linked In、Live Nation、Twitch 和 Chipotle。

lastIndexOf() 方法用於查找數組中最後一次出現的傳遞對象的索引。

用法:

lastIndexOf( object, startAt );

參數:

  • object: 它是我們想要出現的對象。
  • startAt: 它是從哪裏開始查找的索引號。

返回值:數組中對象的最後一個索引;如果未找到,則返回 -1。

第 1 步:要運行以下示例,您需要有一個 ember 項目。要創建一個,您需要先安裝ember-cli。在終端中寫入以下代碼:

npm install ember-cli

第 2 步:現在您可以通過輸入以下代碼來創建項目:

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

示例 1:鍵入以下代碼以生成本示例的路由:

ember generate route lastIndexOf1

應用程序/路線/lastIndexOf1.js

Javascript


import Route from "@ember/routing/route"; 
  
export default class StudentsRoute extends Route { 
    partyItems = [ 
        'Digital Camera', 
        'Jugs, cups & straws', 
        'Balloons', 
        'Cups', 
        'Scissors', 
        'Cold Drink', 
        'Table Confetti', 
        'Party Hats', 
        'Wine', 
        'Napkins', 
        'Scissors', 
        'Party Plates', 
        'Speakers', 
        'Music System', 
        'Cold Drink', 
        'Cups', 
        'Wine', 
    ]; 
    len; 
    model() { 
        return this.partyItems; 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set("partyItems", this.partyItems); 
        controller.set("len", this.len); 
        controller.set("item", this.item); 
    } 
} 

應用程序/控製器/lastIndexOf1.js

Javascript


import Ember from "ember"; 
  
export default Ember.Controller.extend({ 
    actions: { 
        last_index(data) { 
            let ans = this.partyItems.lastIndexOf(data); 
            alert(`Last Index of item is ${ans}`); 
        }, 
        check_items(data) { 
            let temp = this.partyItems.includes(data) 
            alert(temp ? "Item is present in list" :  
                "Item is not Present in list") 
        }, 
        find_index(data) { 
            let temp = this.partyItems.indexOf(data); 
            alert(`Index of ${data} is ${temp}`) 
        }, 
  
    }, 
}); 

應用程序/模板/lastIndexOf1.hbs

Javascript


{{page-title "lastIndexOf"}} 
<h3>List of Items: </h3> 
<table> 
    <ul> 
        {{#each @model as |student|}} 
            <li>{{student}}</li> 
        {{/each}} 
      </ul> 
</table> 
<br /><br /> 
<div> 
    <label>Enter Item: </label> 
      {{input value=this.temp}} 
</div> 
<input 
    type="button"
      id="check-atIndex"
      value="Check Item"
      {{action "check_items" this.temp}} 
/> 
<br /><br /> 
<div> 
    <label>Enter Item: </label> 
      {{input value=this.temp1}} 
</div> 
<input 
    type="button"
      id="find-Index"
      value="Find Index Of Item"
      {{action "find_index" this.temp1}} 
/> 
<br /><br /> 
<div> 
    <label>Enter Item: </label> 
      {{input value=this.temp2}} 
</div> 
<input 
    type="button"
      id="find-last"
      value="Find last Index"
      {{action "last_index" this.temp2}} 
/> 
{{outlet}}

輸出:訪問 localhost:4200/lastIndexOf1 查看輸出。

Ember.js Ember.NativeArray lastIndexOf 方法

示例 2:鍵入以下代碼以生成本示例的路由:

ember generate route lastIndexOf2

應用程序/路線/lastIndexOf2.js

Javascript


import Route from '@ember/routing/route'; 
  
export default class PartyRoute extends Route { 
    partyItems = [ 
        'Oxygen', 
        'The Thing', 
        'Source Code', 
        'Infine', 
        'Ad Astra', 
        'Tenet', 
        'SpiderHead', 
        'The Thing', 
        'A Quiet Place', 
        'The Invisible Man', 
        'Looper', 
        'Infine', 
        'Ad Astra'
    ]; 
    item; 
    idx; 
    len; 
    model() { 
        return this.partyItems; 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set('partyItems', this.partyItems); 
        controller.set('item', this.item); 
        controller.set('idx', this.idx); 
        controller.set('len', this.len); 
    } 
} 

應用程序/控製器/lastIndexOf2.js

Javascript


import Ember from 'ember'; 
import { } from '@ember/array'; 
  
export default Ember.Controller.extend({ 
    actions: { 
        check_index(item) { 
            let temp = this.partyItems.indexOf(item); 
            alert(`Index of Movie is ${temp}`); 
        }, 
        check_movie(item) { 
            let temp = this.partyItems.includes(item); 
            alert(temp ? 'Movie is present in watch list' :  
                'Movie is not Present in watch list'); 
        }, 
        check_last(item) { 
            let temp = this.partyItems.lastIndexOf(item); 
            alert(`Last Index of Movie is ${temp}`); 
        }, 
    }, 
}); 

應用程序/模板/lastIndexOf2.hbs

Javascript


{{page-title "lastIndexOf"}} 
<h3>Here is a list of items: </h3> 
<ul> 
    {{#each @model as |party|}} 
        <li>{{party}}</li> 
    {{/each}} 
</ul> 
<br /><br /> 
<div> 
    <label>Enter Movie: </label> 
    {{input value=this.temp}} 
</div> 
<input 
    type="button"
    id="find-Index"
    value="Find Movie Index"
    {{action "check_index" this.temp}} 
/> 
<br /><br /> 
<div> 
    <label>Enter Movie: </label> 
    {{input value=this.temp1}} 
</div> 
<input 
    type="button"
    id="check-movie"
    value="Check Movie"
    {{action "check_movie" this.temp1}} 
/> 
<br /><br /> 
<div> 
    <label>Enter Item: </label> 
    {{input value=this.temp2}} 
</div> 
<input 
    type="button"
    id="check-last"
    value="Check Last Index of Movie"
    {{action "check_last" this.temp2}} 
/> 
{{outlet}}

輸出:訪問 localhost:4200/lastIndexOf2 查看輸出

Ember.js Ember.NativeArray lastIndexOf 方法

參考:https://api.emberjs.com/ember/4.4/classes/Ember.NativeArray/methods/lastIndexOf?anchor=lastIndexOf



相關用法


注:本文由純淨天空篩選整理自jeemains0neet大神的英文原創作品 Ember.js Ember.NativeArray lastIndexOf() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。