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


Embeer.js EmberArray forEach()用法及代碼示例


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

forEach() 方法是迭代數組並為列表中的每個項目調用回調函數。

用法:

forEach( callback, target );

屬性:

  • callback: 這是為每個項目調用的函數。
  • target:它是哪個函數的項目。

返回值:它返回接收者對象。

安裝步驟:

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

npm install ember-cli

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

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

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

ember generate route forEach1

應用程序/路線/forEach1.js


import Route from '@ember/routing/route'; 
import { } from '@ember/array'; 
  
export default class FruitsRoute extends Route { 
    fruits = [ 
        { 
            'name': 'Lady Finger', 
            'isFruit': false, 
            'color': 'green', 
            'Bucket': 1, 
        }, 
        { 
            'name': 'Brinjal', 
            'isFruit': false, 
            'color': 'purple', 
            'Bucket': 1, 
        }, 
        { 
            'name': 'Apple', 
            'isFruit': true, 
            'color': 'red', 
            'Bucket': 2, 
        }, 
        { 
            'name': 'Grapes', 
            'isFruit': true, 
            'color': 'green', 
            'Bucket': 2, 
        }, 
        { 
            'name': 'Mango', 
            'isFruit': true, 
            'color': 'yellow', 
            'Bucket': 2, 
        }, 
        { 
            'name': 'Watermelon', 
            'isFruit': true, 
            'color': 'red', 
            'Bucket': 2, 
        }, 
        { 
            'name': 'Orange', 
            'isFruit': true, 
            'color': 'orange', 
            'Bucket': 2, 
        } 
    ]; 
    model() { 
        return this.fruits; 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set('fruits', this.fruits); 
    } 
} 

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


import Ember from "ember"; 
import { shiftObject, isAny, isEvery } from "@ember/array"; 
  
export default Ember.Controller.extend({ 
    actions: { 
        Bucket1() { 
            let ans = ''; 
            this.fruits.forEach((item) => 
                ans += (item.Bucket == 1) ? 
                    (item.name + '\n') : ''); 
            alert(ans); 
        }, 
        allFruit() { 
            let ans = ''; 
            this.fruits.forEach((item) => 
                ans += (item.isFruit) ? 
                    (item.name + '\n') : ''); 
            alert(ans); 
        }, 
    } 
}); 

應用程序/模板/forEach1.hbs


{{page-title "Fruits"}} 
<h3>Here is a list of eatables: </h3> 
<ul> 
    {{#each @model as |eatable|}} 
    <li> 
        {{eatable.name}} 
    </li> 
    {{/each}} 
</ul> 
<br /><br /> 
<input type="button" id="fruit-any" 
    value="Item in Bucket 1" 
    {{action "Bucket1" }} /> 
<br /><br /> 
<input type="button" id="fruit-all" 
    value="All Fruits" 
    {{action "allFruit" }} /> 
{{outlet}} 

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

Ember.js EmberArray forEach 方法

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

ember generate route forEach2

應用程序/路線/forEach2.js


import Route from '@ember/routing/route'; 
  
export default class DetailsRoute extends Route { 
    details = [ 
        { 
            name: 'Anubhav', 
            mobile: '1298119967', 
            city: 'Patna', 
            country: 'India', 
            gender: 'M', 
            zipCode: '800020', 
        }, 
        { 
            name: 'Sakshi', 
            mobile: '1234567890', 
            city: 'Mumbai', 
            country: 'India', 
            gender: 'F', 
            zipCode: '400001', 
        }, 
        { 
            name: 'Satyam', 
            mobile: '2222222222', 
            city: 'Delhi', 
            country: 'India', 
            gender: 'M', 
            zipCode: '110012', 
        }, 
        { 
            name: 'Shivam', 
            mobile: '1122113322', 
            city: 'Bangalore', 
            country: 'India', 
            gender: 'M', 
            zipCode: '530068', 
        }, 
        { 
            name: 'Ayushi', 
            mobile: '2244668800', 
            city: 'Jaipur', 
            country: 'India', 
            gender: 'F', 
            zipCode: '302001', 
        }, 
    ]; 
    city; 
    code; 
    model() { 
        return this.details; 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set('details', this.details); 
        controller.set('city', this.city); 
        controller.set('code', this.code); 
    } 
} 

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


import Ember from "ember"; 
import { shiftObject, isAny, isEvery } from "@ember/array"; 
  
export default Ember.Controller.extend({ 
    actions: { 
        Bucket1() { 
            let ans = ''; 
            this.fruits.forEach((item) => 
                ans += (item.Bucket == 1) ? 
                    (item.name + '\n') : ''); 
            alert(ans); 
        }, 
        allFruit() { 
            let ans = ''; 
            this.fruits.forEach((item) => 
                ans += (item.isFruit) ? 
                    (item.name + '\n') : ''); 
            alert(ans); 
        }, 
    } 
}); 

應用程序/模板/forEach2.hbs


{{page-title "Fruits"}} 
<h3>Here is a list of eatables: </h3> 
<ul> 
    {{#each @model as |eatable|}} 
    <li> 
        {{eatable.name}} 
    </li> 
    {{/each}} 
</ul> 
<br /><br /> 
<input type="button" id="fruit-any" 
    value="Item in Bucket 1" 
    {{action "Bucket1" }} /> 
<br /><br /> 
<input type="button" id="fruit-all" 
    value="All Fruits" 
    {{action "allFruit" }} /> 
{{outlet}} 

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

Ember.js EmberArray forEach 方法

參考:https://api.emberjs.com/ember/4.6/classes/EmberArray/methods/forEach?anchor=forEach



相關用法


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