当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。