2

I have an allItems array of objects

allItems = [
    { id: 1, name: 'item1' },
    { id: 2, name: 'item2' },
    { id: 3, name: 'item3' }
]

I want it to filter out and not contain the objects contained in another array from my component.

fewItems = [
    { id: 2, name: 'item2' }
]

so, filteredItems should be :

filteredItems = [
    { id: 1, name: 'item1' },
    { id: 3, name: 'item3' }
]

And when another object from allItems is added to fewItems, it needs to disappear from the filteredItems one.

I would like to do this in pure vanilla JS, with no specific library.

Thanks ahead !

5
  • what is the output u want Commented Oct 23, 2017 at 17:12
  • Edited the question Commented Oct 23, 2017 at 17:13
  • 1
    Do you want to be able to do this with just JavaScript or with a library? Commented Oct 23, 2017 at 17:32
  • @Chris you're right I should have specified this in the question. Edited again. Commented Oct 23, 2017 at 18:22
  • 1
    @JeremyBelolo I think you made a much better question with those simple changes. Commented Oct 24, 2017 at 14:14

4 Answers 4

2
filteredItems =  this.allItems.filter(x => {
    return !this.fewItems.some(y => JSON.stringify(y) == JSON.stringify(x))
});
Sign up to request clarification or add additional context in comments.

1 Comment

just be carefull with stringify stackoverflow.com/questions/15376185/….
1

var allItems = [
     { id: 1, name: 'item1' },
        { id: 2, name: 'item2' },
        { id: 3, name: 'item3' }
    ];
    
    var fewItems = [
        { id: 2, name: 'item2' }
    ];
    
    var keys = Object.keys( fewItems[0] ) 
    
    var result = allItems.filter( 
      function(item){ 
        for( var k = fewItems.length-1; k>=0; --k){ 
          var dontWant = fewItems[k]; 
          var i=keys.length-1; 
          for( ; i>=0; --i ){ 
            if( dontWant[keys[i]] != item[keys[i]]){  break; } 
          } 
          if( i == -1){ return false;} 
        } 
        return true; 
      }
    );

    console.log(result)

Comments

1

Pure JS solution

var allItems = [
  { id: 1, name: 'item1' },
  { id: 2, name: 'item2' },
  { id: 3, name: 'item3' }
];

var fewItems = [
  { id: 2, name: 'item2' }
];

var result3 = allItems
  .filter(item => {
    for(let restrictedItem of fewItems) {
      if (JSON.stringify(restrictedItem) === JSON.stringify(item)) {
        return false;
      }
    }
    return true;
  })

console.log(result3);

Comments

1

var allItems = [
    { id: 1, name: 'item1' },
    { id: 2, name: 'item2' },
    { id: 3, name: 'item3' }
];

var fewItems = [
    { id: 2, name: 'item2' }
];

var result3 = _(allItems) 
        .differenceBy(fewItems, 'id', 'name')
        .map(_.partial(_.pick, _, 'id', 'name'))
        .value();

console.log(result3);

 
<script src='https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js'></script>

EDIT

Without Lodash

var allItems = [
    { id: 1, name: 'item1' },
    { id: 2, name: 'item2' },
    { id: 3, name: 'item3' }
];

var fewItems = [
    { id: 2, name: 'item2' }
];


var props = ['id', 'name'];
var result = allItems.filter(function(o1){
     return !fewItems.some(function(o2){
        return o1.id === o2.id;        
    });
}).map(function(o){
     return props.reduce(function(newo, name){
        newo[name] = o[name];
        return newo;
    }, {});
});

console.log(result);

3 Comments

Hey, thanks for this... But I don't like the idea of integrating lodash in an Angular app. I would really like to do it without, if at all possible.
@JeremyBelolo check the solution without lodash, edited answer
So you say it's an Angular app? so probably best to use Angular's Comparison methods for equality, since you don't wnat to use Lodash: docs.angularjs.org/api/ng/function/angular.equals I thought you put in original quesiton Angular but now it's gone.

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.