[Vuejs]-Vue.js โ€“ Filter array of JSONs by another array of JSONs

1๐Ÿ‘

โœ…

try filter and find methods :

let result =products.filter(prod=>{
   return !deletedProducts.find(dprod=>{
         return dprod.id===prod.id;
    })

})

let products = [{
    id: 1,
    name: 'Box'
  },
  {
    id: 2,
    name: 'Lamp'
  }
]

let deletedProducts = [{
  id: 1,
  name: 'Box'
}]

let result = products.filter(prod => {
  return !deletedProducts.find(dprod => {
    return dprod.id === prod.id;
  })

})

console.log(result)

0๐Ÿ‘

try this comparer function and filter. (reference by array of element "id" on this exmaple)

 let products = [
  {
    id: 1,
    name: 'Box'
  },
  {
    id: 2,
    name: 'Lamp'
  }
]

let deletedProducts = [
  {
    id: 1,
    name: 'Box'
  }
]

function comparer(otherArray){
      return function (current) {
          return otherArray.filter(function(other) {
              return other.id === current.id
          }).length===0;
        }
    }


var result=products.filter(comparer(deletedProducts ));

console.log(result);

Leave a comment