【问题标题】:Filter array matching all object keys. Get new array with unique objects匹配所有对象键的过滤器数组。获取具有唯一对象的新数组
【发布时间】:2017-03-27 11:57:26
【问题描述】:

我想根据包含对象的现有数组获取 2 个新数组。 (每个对象的键保持不变)

newArray1 对象的值应该是唯一的。换句话说:

[ 
   {Layer:"Layer",LayerName:"foo",LayerWidth:200,LayerHeight:400},
   {Layer:"Layer",LayerName:"FOOBAR",LayerWidth:200,LayerHeight:400},       
   {Layer:"Layer",LayerName:"foo",LayerWidth:200,LayerHeight:400}
]
  • newArray1 应该由第一个和第二个对象组成,因为第三个对象与第一个对象具有相同的对象值。

  • newArray2 应该包含所有其他不在 newArray1 中的对象,(仅限第三个对象)

arr = []

for(var i=0;i<allSymbols.length;i++){
    var obj={
      Layer: allSymbols(i),
      LayerName: allSymbols(i).name(),
      LayerWidth: allSymbols(i).width(),
      LayerHeight: allSymbols(i).height()
    }
    arr.push(obj)
}


// filter so far... 
for(var u=0;u<arr.length;u++){
    var filter = arr[u];
    arr = arr.filter(function(item){
     for(var key in filter){
       if(item[key] == filter[key]){
        return true
       }
     }
       return false
    });
}

// output newArray1(arr)?
// output newArray2?

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    你可以试试这样的:

    逻辑

    • 获取每个对象的过滤列表。
    • 获取第一个匹配对象的索引。
    • 如果过滤列表的长度为 1,则对象是唯一的,应推送到唯一列表。
    • 如果不是并且索引等于当前索引,则这是第一次出现,应再次推送到唯一列表。
    • 对于所有剩余的对象,它们都是重复的,并且是第二次或第 n 次重复,应该被推送到重复列表中

    var d = [{Layer:"Layer",LayerName:"foo",LayerWidth:200,LayerHeight:400},{Layer:"Layer",LayerName:"FOOBAR",LayerWidth:200,LayerHeight:400},{Layer:"Layer",LayerName:"foo",LayerWidth:200,LayerHeight:400}]
    
    var dupes = [];
    var unique = [];
    
    d.forEach(function(c,i,a){
      var objStr = JSON.stringify(c);
      
      var index = a.findIndex(function(o){
        return JSON.stringify(o) === objStr;
      });
      
      var _filter = a.filter(function(o){
        return JSON.stringify(o) === objStr;
      });
      if(_filter.length === 1 || index === i){
        unique.push(c)
      } 
      else{
        dupes.push(c);
      }
    }, []);
    
    console.log(dupes, unique)

    【讨论】:

      【解决方案2】:

      您可以检查重复的对象并过滤给定的数据。

      var data = [{ Layer: "Layer", LayerName: "foo", LayerWidth: 200, LayerHeight: 400 }, { Layer: "Layer", LayerName: "FOOBAR", LayerWidth: 200, LayerHeight: 400 }, { Layer: "Layer", LayerName: "foo", LayerWidth: 200, LayerHeight: 400 }],
          array1 = [],
          array2 = data.filter(function (a) {
              return array1.some(function (b) {
                  return Object.keys(a).length === Object.keys(b).length && Object.keys(a).every(function (k) {
                      return a[k] === b[k];
                  });
              }) || array1.push(a) && false;
          });
      
      console.log(array1);
      console.log(array2);
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

        猜你喜欢
        • 2021-02-26
        • 2019-02-08
        • 1970-01-01
        • 1970-01-01
        • 2019-04-11
        • 1970-01-01
        • 2021-12-23
        • 2017-01-07
        • 2017-11-16
        相关资源
        最近更新 更多