剔除数组getdistinct对象的值 [英] knockout arraygetdistinctvalues of objects

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

问题描述

我想在数组中的多个属性上像此小提琴一样使用ko.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);

我在这里修改过的小提琴

有人可以告诉我该怎么做吗?

Can someone tell me how to do this?

推荐答案

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/

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

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