简体   繁体   English

在Ember中,如何通过检查一个键/值从数组中获取对象的所有属性

[英]In Ember, how to get all the properties of an object from an Array by checking one key/value

I have an array of objects of products. 我有一系列的产品对象。 But I want to grab just one object in the array to do something with it, by checking a property value of that object if it's true . 但是我想通过检查该对象的属性值是否为true来仅捕获数组中的一个对象以对其进行处理。

I'm not sure if I'm doing it correctly. 我不确定自己是否做得正确。 How can I do that the Ember way. 我该怎么做?

Can someone help me, please. 有人能帮助我吗。 Thank you! 谢谢!

Model: 模型:

// model
import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
  sku: DS.attr('number'),
  price: DS.attr('number'),
  createdAt: DS.attr('date')
});

Controller: 控制器:

// controller
import Ember from 'ember';

export default Ember.Controller.extend({
  store: Ember.inject.service(),

  products: Ember.computed('store', function () {
    return this.get('store').findAll('product').then(product => product);
  }),

  actions: {
    selectProduct(value) {
      // I'm not sure if this is the correct way of grab an object
      this.get('products').filter((product) => {

        if (product.get('name') === value) {
          let newProduct = Ember.Object.create({
            name: product.get('name'),
            description: product.get('description'),
            price: product.get('price')
          });

          return newProduct;
        } else {
          console.log('Please select something!!');
        }
      });
    }
  }
});

Template: 模板:

// template
<div class="product-list">
  <select id="prod-list" onchange={{action "selectProduct" value="target.value"}}>
    {{#each products as |product|}}
      <option value="{{product.name}}">{{product.name}}</option>
    {{/each}}
  </select>
</div>

At first, it's better to fetch data in route. 首先,最好在路由中获取数据。

You can use findBy method to find an item. 您可以使用findBy方法查找项目。

Please take a look at this twiddle . 请看一下这个玩笑

I changed the adapter to fake data. 我将适配器更改为伪造数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个键中获取一个对象,数组中的值 - How to get an object from one key,value in array 如何从所有对象中删除一个简单的特定键值对并添加到数组内的一个特定 object - How to remove a simple specific key value pair from all objects and add to one specific object inside an array 如何从Key Json中获得一对一的值(数组中的值)? - How to get one by one values(value in array) from Key Json? Ember-从Ember数据数组获取价值 - Ember - Get Value From Ember Data Array 如何从 object 的数组中获取某些键值对 - How to get certain key value pairs from an array of object Javascript 如何从数组 Object 中分别获取值和键? - Javascript how to get value and key seperately from Array Object? 如何从具有特定键值的对象数组中获取 object? - How to get an object from an array of objects with a specific value for a key? 如何将一个对象数组转换为一个包含一个 object 的数组,其中键是固定的,值是所有原始对象? - How to convert an array of objects into an array with one object with the key being fixed and value being all original objects? 如何使用javascript从数组对象中获取所有值? - How to get all value from array object using javascript? 如何获取公共键中的对象值数组 - How to get object value array in common key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM