0

Hey I need to map this array of products. I want to get the full objects after search with a specific value of the code property.

For example, how do I find all objects where the code is "balance"?

"products": [{
  "code": "balance",
  "currency": "ILS",
  "percentage": 1,
  "totalWorth": 150000,
  "totalYieldPer": 0,
  "items": [{
    "name": "בנק של עומרי",
    "currency": "NIS",
    "worth": 150000,
    "portfolioPer": 1,
    "yield": 0,
    "yieldPer": 0
  }]
}, {
  "code": "debt",
  "currency": "ILS",
  "percentage": 1,
  "totalWorth": 150000,
  "totalYieldPer": 0,
  "items": [{
    "name": "משכנתא על הדירה שלי",
    "currency": "ILS",
    "worth": 150000,
    "portfolioPer": 1,
    "yield": 0,
    "yieldPer": 0
  }]
}, {
  "code": "pension",
  "currency": "ILS",
  "percentage": 1,
  "totalWorth": 150000,
  "totalYieldPer": 0,
  "items": [{
    "name": "פנסיוני",
    "currency": "ILS",
    "worth": 150000,
    "portfolioPer": 1,
    "yield": 0,
    "yieldPer": 0
  }]
}]
}
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Omri Dan
  • 15
  • 1

1 Answers1

0

As far as my understanding, this solution would suffice:

var resObj = {};
products.forEach((item) => {
  resObj[item.code] = item;
});

console.log(resObj);
Suman B K
  • 1
  • 4