1

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", 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", …}]}

The list is about 400 products long.

Say I want to limit the list showing only specific products based on the field ProduktID (which is unique). Let's say I have a list of products in an offer:

07.1438, 01, 1340, 05, 04531, 02.0135

Where comma is the separator.

How can I .grep or .map the objects array based on these inputs? Do I have to search one by one and then combine the results into another object? Or is there a faster way?

2 Answers 2

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

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

1 Comment

I still get error on the "filter" keyword. Error sais: "products.filter is not a function"
1

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

6 Comments

It really looks like a great answer. Thank you. I will test it.
BUT where does the word "PRODUCT" come into play? you call the array "productS" I see no mention of "product" anywhere in the code except in the const filtered ??
products.filter((----->product<-----) => { ... }) - it is an argument of the array function passed to the .filter() method
I get an error: "filter is not a function". when I try to add products.filter(...) Do I have to convert the products array in some special manner ?
i see now that my array is formatted sligthly different than in my initial question: it is really: {"products":[{"AdminID":491,"ProduktID":"01.0635","itemName":"ClearBond .... }]...}
|

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.