簡體   English   中英

Backbone.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));

少量按鍵和少量項目的性能似乎很好,但您需要使用一百萬條記錄和大量按鍵進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM