【问题标题】:Comparing Objects and getting only ones that exist in all比较对象并仅获取所有对象中存在的对象
【发布时间】:2015-11-05 02:25:09
【问题描述】:

我正在为Mixitup 构建一个过滤方法,我需要能够正确过滤 x 个选定的参数。 (这里的插件并不重要,重要的是我如何获得正确的最终对象)

我目前有一个对象,它为每个唯一搜索发送一个唯一键,并将其匹配对象(通过 filter 方法获得)发送到我的过滤函数中。

这就是我迷路的地方。

我需要能够遍历我的对象,它是关联的 key => 值(对象),并且只提取每个对象中存在的对象。

例如,(我有 jQuery 对象而不是数字)

var filter = {
    x : {1,3,5,6},
    y : {1,4,7,8},
    z : {1,9}
}

根据上面的例子,唯一返回的对象是 - 1(因为它是唯一存在于所有三个键中的对象。

任何帮助将不胜感激!

【问题讨论】:

  • xyz 应该是数组吗?它们是目前只有属性的对象字面量。
  • 您的意思是“唯一返回的 value 将是 1(因为它是唯一存在于所有三个 arrays 中的)”?等等,为什么它们是 jQuery 对象?我不认为他们是顺便说一句。它们不是任何一种 JS 结构。
  • 数字是 jquery 对象...它的测试数据...不会粘贴整个对象并期望人们只是这样做...这只是懒惰!

标签: javascript jquery arrays sorting multidimensional-array


【解决方案1】:

Array.reduceArray.filter 的简短方法:

基本上它从第一个数组开始作为 reduce 的起始值。然后通过索引查找过滤结果集,如果找到,则保留该值,否则跳过该值。这种情况一直持续到对象不再具有属性为止。

var filter = {
    x: [1, 3, 5, 6],
    y: [1, 4, 7, 8],
    z: [1, 9]
};

var filtered = Object.keys(filter).reduce(function (r, a, i) {
    return i ? r.filter(function (b) {
        return ~filter[a].indexOf(b);
    }) : filter[a];
}, []);

document.write('<pre>' + JSON.stringify(filtered, 0, 4) + '</pre>');

对象加分,返回公共键/s:

var filter = {
    x: { a: 'ah', c: 'ce', e: 'eh', f: 'ef' },
    y: { a: 'ah', d: 'de', g: 'ge', h: 'ha' },
    z: { a: 'ah', i: 'ie' }
};

var filtered = Object.keys(filter).reduce(function (r, a, i) {
    return i ? r.filter(function (b) {
        return b in filter[a];
    }) : Object.keys(filter[a]);
}, []);

document.write('<pre>' + JSON.stringify(filtered, 0, 4) + '</pre>');

【讨论】:

    【解决方案2】:

    所以,我会分两部分进行:

    1 查找常见项目

    2 提取常用项

    var filter = {
        x : [1,3,5,6],
        y : [1,4,7,8],
        z : [1,9]
    }
    
    // Find the common
    var common;
    Object.keys(filter).forEach(function (k) {
        if (common) {
             for (var i=0; i<common.length; i++) {
                 // Check if the common value exists on the key
                 if (filter[k].indexOf(common[i]) === -1) {
                     // If it does not, it is not common
                     common.splice(i, 1);
                     i--;
                 }
             }
        } else {
            // On the first item, we assume all are common
            common = filter[k]
        }
    })
    
    // Additional processing can take place to extract the data you need here
    //Should print "[1]"
    console.log(common)
    

    【讨论】:

      【解决方案3】:

      这是一个解决方案:这里我使用了与您的数据相同的结构,没有将其转换为数组

      HTML

      <p id="result"></p>
      

      JavaScript

      var value = 'value',
          filter = {
              x : {1:value,3:value,5:value,6:value,9:value},
              y : {1:value,4:value,7:value,8:value,9:value},
              z : {1:value,9:value}
          };
      
      function getCommon(data) {
          var subObjects = [],
              common = [];
      
          if(typeof data === 'object'){
      
              // collect all sub-keys into an array
              Object.keys(data).forEach(function(key){
                  if(typeof data[key] === 'object') {
                      subObjects = subObjects.concat(Object.keys(data[key]));
                  }
              });
      
              // get the common keys
              Object.keys(data[Object.keys(data)[0]]).forEach(function(subKey){
                  if(getCount(subObjects, subKey) === Object.keys(data).length) {
                      common.push(subKey);
                  }
              });
      
              function getCount(data, target){
                  return data.filter(function(item){
                      return item === target;
                  }).length;
              }
      
              return common;
          }
      }
      
      document.getElementById('result').innerHTML = getCommon(filter);
      

      JSFiddle:http://jsfiddle.net/LeoAref/bbnbfck7/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-01
        • 1970-01-01
        • 2018-01-09
        • 1970-01-01
        • 2018-03-29
        • 2020-01-15
        • 1970-01-01
        相关资源
        最近更新 更多