1

I have an array of objects. In each object there is a details property (array) with more than one object - for this example I am only showing one.

I am looking for the rating property in each details and looking to find minimum value (in this example 3.1) ... is there a simpler/cleaner way of achieving this?

const ratings = [{ id: 'ABC', details: [{ type: 'VALUE', rating: 9.5 }] }, { id: 'DEF', details: [{ type: 'VALUE', rating: 3.1 }] }, { id: 'GHI', details: [{ type: 'VALUE', rating: 4.5 }] }]
const ids = ['ABC', 'DEF', 'GHI']
const array = []

ids.forEach(element => {
  const valueScore = ratings?.find(r => r.id === element)?.details?.find(c => c.type === 'VALUE')
  if (valueScore.rating) {
    array.push(valueScore.rating)
  }
})
console.log('MIN VALUE', Math.min(...array))
1
  • Instead of iterating over the ids and then using find why not just iterate once through ratings, also if your code works and you want feedback/suggestions you might want to look at the code review SE site Commented Feb 26, 2021 at 16:50

2 Answers 2

1

You can use flatMap and then apply math.min to get minimum value:

const ratings = [{ id: 'ABC', details: [{ type: 'VALUE', rating: 9.5 }] }, { id: 'DEF', details: [{ type: 'VALUE', rating: 3.1 },{ type: 'VALUE1', rating: 2.1 }] }, { id: 'GHI', details: [{ type: 'VALUE', rating: 4.5 }] }];

const ids = ['ABC', 'DEF', 'GHI']

console.log(Math.min(...ratings.flatMap(o=>
    ids.includes(o.id) ? o.details.flatMap(p=>
        p.type=='VALUE' ? p.rating : []) : [])));

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

4 Comments

I am looking for rating of type VALUE though. It's possible there would be another type (e.g. { type: 'TEST', rating: 4 } ... and I don't want to look at that
Thanks! Also, we need to check that the minimum rating is an id that is found in ids array ... if there is another entry in ratings such as XYZ, this shouldn't be included. can you modify?
@Michael that was a simple check in flatMap. Updated.
Thanks so much :) Simple and clean. I always forget about flatMap
0

You can do something like this:

Why this method is preferred is, you are running the loop perfectly over each ratings item and also through each details array item.

  ratings = [
    { id: "ABC", details: [{ type: "VALUE", rating: 9.5 }] },
    {
      id: "DEF",
      details: [{ type: "VALUE", rating: 3.1 }, { type: "VALUE1", rating: 2.1 }]
    },
    { id: "GHI", details: [{ type: "VALUE", rating: 4.5 }] }
  ];

  ids = ["ABC", "DEF", "GHI"];
    const detailsRatingArr = [];
    if (ratings.length === ids.length) {
      ratings.forEach(eachRating => {
        ids.forEach(eachId => {
          if (eachRating.id === eachId) {
            eachRating.details.forEach(eachDetail => {
              detailsRatingArr.push(eachDetail.rating);
            });
          }
        });
      });
    }
    console.log("DETAILS ARRAY ==>", detailsRatingArr);
    detailsRatingArr.sort((a, b) => a - b);
    console.log("Minimum Rating ==>", detailsRatingArr[0]);
  

Comments

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.