Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

generic product filter javascript

const compare = r => l => (typeof l === "object" ? contains(r)(l) : l === r);
const contains = r => l =>
  Object.keys(r).every(k => l.hasOwnProperty(k) && compare(r[k])(l[k]));

var list = [
  {
    name: "Alice",
    address: {
      zip: "10000"
    }
  },
  {
    name: "Bob",
    address: {
      zip: "20000"
    }
  },
  {
    name: "Charlie",
    address: {
      zip: "30000"
    }
  }
];

var filter = {
  address: { zip: "10000" }
};

console.log(list.filter(contains(filter)));
// [{"name":"Alice","address":{"zip":"10000"}}]
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #generic #product #filter #javascript
ADD COMMENT
Topic
Name
1+9 =