2

I would like to get 2 new arrays based on an existing array containing objects. (keys stay the same for each object)

newArray1 objects should be unique in terms of their value. In other words:

[ 
   {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 should consist of 1st and 2nd object, because 3rd has the same object-values like the 1st object.

  • newArray2 should contain all other objects, that are not in newArray1, (3rd object only)

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?

2 Answers 2

1

You can try something like this:

Logic

  • Get filtered list for every object.
  • Get index of first matching object.
  • If length of filtered list is one, object is unique and should be pushed to unique list.
  • If not and if index is equal to current index, this is the first occurrence and again should be pushed to unique list.
  • For all remaining objects, they are duplicates and are second or nth repetition and should be pushed to duplicate list

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)

Sign up to request clarification or add additional context in comments.

Comments

1

You could check for duplicate objects and filter the given data.

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.