javascript – Backbone.js:比较集合中的模型属性
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – Backbone.js:比较集合中的模型属性,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2615字,纯文字阅读大概需要4分钟。
内容图文
![javascript – Backbone.js:比较集合中的模型属性](/upload/InfoBanner/zyjiaocheng/792/b50d7748d2ff4478a037a8519cb6d3de.jpg)
我有一个无限范围的集合(可以是0,可以是1000,可以是一百万).我需要搜索集合中每个模型的属性,并返回相同的属性(及其值).
例如,如果我在集合中有以下三个模型:
modelOne:
color: "red"
age: 10
size: "large"
modelTwo:
color: "red"
age: 11
size: "medium"
modelThree:
color: "red"
age: 9
size: "large"
我需要应用程序返回颜色:“红色”(或其他一些可以解析的派生),因为它是所有三个模型中唯一相同的属性.
编辑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));
少量按键和少量项目的性能似乎很好,但您需要使用一百万条记录和大量按键进行测试.
内容总结
以上是互联网集市为您收集整理的javascript – Backbone.js:比较集合中的模型属性全部内容,希望文章能够帮你解决javascript – Backbone.js:比较集合中的模型属性所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。