Skip to content Skip to sidebar Skip to footer

Jquery Grep Or Map Object Array With Multiple Search Criterias

I have a price list in JSON: {Products: [{AdminID: 137, ProduktID: '07.1434', itemName: 'Repaplast', itemColor: '0000, 5030', MalKode: '1-3', …}{AdminID: 6, ProduktID: '07.1436

Solution 1:

You can use array#filter and array#find. It will result in filtering the products based on the product ids string.

const productIds = '07.1438, 01,1340, 05,04531, 02.0135';

const products = [{AdminID: 137, ProduktID: "07.1438", itemName: "Repaplast", itemColor: "0000, 5030", MalKode: "1-3",},{AdminID: 6, ProduktID: "07.1436", itemName: "Repaplast grå", itemColor: "0070", MalKode: "1-3",},{AdminID: 146, ProduktID: "90.0905", itemName: "Mixer Gun", itemColor: null, MalKode: "",},{AdminID: 89, ProduktID: "02.0135", itemName: "Repaplast Primer NEW FORMULA", itemColor: "", MalKode: "5-3",}];

var result = products.filter( o => productIds.split(',').find(productId => o.ProduktID === productId.trim()));

console.log(result);

Solution 2:

Use Array.prototype.filter() combined with Array.prototype.includes():

const wanted   = ['07.1438', '01','1340', '05', '04531', '02.0135'];
const products = [
  {
    AdminID  : 137, 
    ProduktID: "07.1434", 
    itemName : "Repaplast", 
    itemColor: "0000, 5030", 
    MalKode  : "1-3"
  },
  {
    AdminID  : 6, 
    ProduktID: "07.1436", 
    itemName : "Repaplast grå", 
    itemColor: "0070", 
    MalKode  : "1-3"
  },
  {
    AdminID  : 146, 
    ProduktID: "90.0905", 
    itemName : "Mixer Gun", 
    itemColor: null, 
    MalKode  : ""
  },
  {
    AdminID  : 89, 
    ProduktID: "02.0135", 
    itemName : "Repaplast Primer NEW FORMULA", 
    itemColor: "", 
    MalKode  : "5-3"
  }
];

const filtered = products.filter((product) => wanted.includes(product.ProduktID));
console.log("Filtered", filtered);

Post a Comment for "Jquery Grep Or Map Object Array With Multiple Search Criterias"