主干.js:比较集合中的模型属性
Backbone.js: compare model attributes in a collection
我有一个无限范围的集合(可以是0,可以是1000,可以是一百万)。 我需要搜索集合中每个模型的属性,并返回相同的属性(及其值)。
例如,如果我的集合中有以下三个模型:
modelOne:
color: "red"
age: 10
size: "large"
modelTwo:
color: "red"
age: 11
size: "medium"
modelThree:
color: "red"
age: 9
size: "large"
我需要应用程序返回color: "red"
(或其他一些可以解析的派生),因为它是唯一在所有三个模型中相等的属性。
编辑 John Munsch 的解决方案运行良好,但现在要求发生了变化,因为某些属性现在可以是数组。 有没有办法比较常规属性和数组属性?
新代码示例:
modelOne:
color: "red"
age: 10
sizes: ["small", "large"]
modelTwo:
color: "red"
age: 9
sizes: ["small", "large"]
这里有一个快速的jsFiddle,我的答案版本: http://jsfiddle.net/JohnMunsch/3NMGD/
注意:jsFiddle 和下面的代码都已更新,以反映问题更改的要求。
var model = [
{
color: "red",
age: 10,
size: [ "small", "large" ]
},
{
color: "red",
age: 11,
size: [ "small", "large" ]
},
{
color: "red",
age: 9,
size: [ "small", "large" ]
}
];
function findCommonalities(data) {
if (data.length > 0) {
// It's safe enough to get the list of keys from the very first
// element. If the later ones differ, you know that the keys they
// had but the first element didn't are guaranteed not to be in
// the common set anyway because the first element didn't
// have them.
var keys = _.keys(data[0]);
var commonalities = { };
_.each(keys,
function (key) {
var values = _.pluck(data, key);
if (values.length == data.length) {
// Sadly calling _.uniq() won't work when the values
// plucked out are arrays themselves. It calls ===
// and that's not sufficient for arrays.
if (_.isArray(values[0])) {
// However, we can get a little tricky here.
// What if we _.zip() together all of the arrays
// (assuming the ordering for each array is the
// same) then we'll end up with an array of
// arrays where each one can again be tested
// with _.uniq() because each one will contain
// the same value taken from each array.
var zippedValues = _.zip(values);
console.log("zippedValues", zippedValues);
if (!_.find(zippedValues,
function (zippedValue) {
var uniqueValues = _.uniq(zippedValue);
// Note: This test is the inverse of the
// one below. We're trying to find any
// array that has more than one value in
// it. If we do then there's some
// variance.
return uniqueValues.length != 1;
})) {
// We didn't find any arrays that had any
// variance so we want this as well.
commonalities[key] = values[0];
}
} else {
var uniqueValues = _.uniq(values);
if (uniqueValues.length == 1) {
commonalities[key] = uniqueValues[0];
}
}
}
}
);
return commonalities;
} else {
return { };
}
}
console.log("commonalities: ", findCommonalities(model));
使用少量键和少量项时性能似乎不错,但您需要使用一百万条记录和大量键对其进行测试。
相关文章:
- 将输入值设置为ng模型属性[Angular]时出现问题
- 如何使用Undercore和Backbone在HTML中表示模型属性
- 主干中的模型属性未定义
- sails.js beforeCreate方法只接收required设置为true的模型属性
- 比较主干中的两个模型属性
- 更新淘汰视图模型属性时激发更改事件
- Eloquent模型属性为骆驼大小写[Laravel 5.2][Dingo API]
- 如何从单独的javascript文件访问模型属性?(MVC)
- 从Ember视图获取模型属性
- 根据三个模型属性对主干集合进行排序
- 根据模型属性对主干集合进行排序
- 默认情况下,一个模型属性等于 sail.js 模型中的另一个模型属性
- 在 Backbone 中侦听和修改模型属性的正确方法是什么.js
- 如何从视图主干传递回模型属性
- 主干 js - 更改模型属性
- AngularJs 将模型属性绑定到复选框
- 主干.js - 如何在模板中使用自定义模型属性
- 主干 + 下划线:找不到模板变量.+ 如何正确访问模型属性
- 从另一个具有属于关系的模型访问模型属性 Ember.js
- 通过模型属性 AngularJS 获取节点元素