剔除数组获取对象的不同值 [英] knockout arraygetdistinctvalues of objects

查看:17
本文介绍了剔除数组获取对象的不同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 this fiddleko.utils.arrayGetDistinctValues> 在数组中的多个属性上,因此我将数组映射到仅包含我想要的两个属性的数组

I want to use ko.utils.arrayGetDistinctValues like in this fiddle on more than one property in an array so I map the array to an array of just the two properties I want

viewModel.justCategories = ko.dependentObservable(function() {
    var categories = ko.utils.arrayMap(this.items(), function(item) {
        return { catid : item.catid(), category : item.category() };
    });
    return categories.sort();
}, viewModel);

然后我尝试使用 arrayGetDistinctValues 但它似乎不适用于对象.

then I try to use arrayGetDistinctValues but it doesn't seem to work on objects.

viewModel.uniqueCategories = ko.dependentObservable(function() {
    return ko.utils.arrayGetDistinctValues(viewModel.justCategories()).sort();
}, viewModel);

我在这里修改的小提琴

谁能告诉我怎么做?

推荐答案

arrayGetDistinctValues 仅适用于原始值.对于对象,您将需要不同的方法.这是一个有效的版本.

arrayGetDistinctValues only works with primitive values. For objects, you'll need a different approach. Here's a version that works.

viewModel.uniqueCategories = ko.dependentObservable(function() {
    var seen = [];
    return viewModel.justCategories().filter(function(n) {
        return seen.indexOf(n.catid) == -1 && seen.push(n.catid);
    });
});

http://jsfiddle.net/mbest/dDA4M/2/

这篇关于剔除数组获取对象的不同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆