小编lee*_*489的帖子

Lodash groupBy on object preserve键

在对象键上使用Lodash _.groupBy方法时,我想保留键.

假设我有对象:

foods = {
    apple: {
        type: 'fruit',
        value: 0
    },
    banana: {
        type: 'fruit',
        value: 1
    },
    broccoli: {
        type: 'vegetable',
        value: 2
    }
}
Run Code Online (Sandbox Code Playgroud)

我想做一个转换来获得输出

transformedFood = {
    fruit: {
        apple: {
            type: 'fruit',
            value: 0
        },
        banana: {
            type: 'fruit',
            value: 1
        }
    },
    vegetable: {
        broccoli: {
            type: 'vegetable',
            value: 2
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

transformedFood = _.groupBy(foods, 'type')了以下输出:

transformedFood = {
    fruit: {
        {
            type: 'fruit',
            value: 0
        },
        {
            type: 'fruit', …
Run Code Online (Sandbox Code Playgroud)

javascript lodash

8
推荐指数
1
解决办法
4820
查看次数

Ember.js计算属性过滤器hasMany数组

我试图过滤ember-data'hasMany'字段的内容.我的模型有一些子参数,我想在我的控制器上过滤到属性'childOptions'并在模板中显示

{{#each childOptions}}stuff{{/each}}
Run Code Online (Sandbox Code Playgroud)

当我把它放在我的控制器上时,它工作,每个迭代适当的值:

childOptions: Ember.computed.filterBy('model.subquestions', 'surveyQuestionType.name', 'childOption'),
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时,没有显示任何内容.

childOptions: Ember.computed.filter('model.subquestions', function(subquestion) {
    return subquestion.get('surveyQuestionType.name') === 'childOption';
}),
Run Code Online (Sandbox Code Playgroud)

'surveyQuestionType'是DS.belongsTo,它存在于'subquestions'模型中,并且具有'name'属性.

我想理解为什么'filterBy'方法有效,而'filter'方法却没有(因此我将来可以使用'filter'来处理更复杂的查询).我认为这与promises和subquestion.get('property')我在filter函数中使用的语法有关.

编辑:

这是模型:

App.SurveyQuestion = DS.Model.extend(Ember.Validations.Mixin, {
    surveyQuestionType: DS.belongsTo('surveyQuestionType', { async: true }),
    display: DS.belongsTo('surveyQuestionDisplay', { async: true, inverse: 'surveyQuestion' }),
    sortOrder: DS.attr('number'),
    parent: DS.belongsTo('surveyQuestion', { async: true, inverse: 'subquestions' }),
    parentDependencyCriteria: DS.attr('string'),
    required: DS.attr('boolean'),
    surveySections: DS.hasMany('surveySectionQuestion', { async: true, inverse: 'surveyQuestion' }),
    subquestions: DS.hasMany('surveyQuestion', { async: true, inverse: 'parent' })
});
Run Code Online (Sandbox Code Playgroud)

ember.js ember-data

3
推荐指数
1
解决办法
2763
查看次数

标签 统计

ember-data ×1

ember.js ×1

javascript ×1

lodash ×1