2

Here we have an array of objects:

var slides = [
{ id: 1, performance: 20, guided_phrases: ["I was sent", "I don't know"] },
{ id: 2, performance: 30, guided_phrases: ["to earth"] },
{ id: 3, performance: 40, guided_phrases: ["to protect you"] },
{ id: 4, performance: 15, guided_phrases: ["I was sent"] },
{ id: 5, performance: 50, guided_phrases: ["I was sent"] }
];

I want to find the id of the slide which its guided_phrases includes a specific string (represented in phrase variable ) with the lowest performance...

So if we have let phrase = "I was sent"; then the output should be slide id 4 because the performance is the lowest among slides 1 and 4 and 5.

I have tried this code but I need a hand to figure out a simple way to log the id:

let phrase = "I was sent";
let contain = [];
for(let i = 0; i < slides.length; i++){

   if(slides[i].guided_phrases.includes(phrase)){

     contain.push(slides[i].performance)
     console.log(contain)
     let max = Math.min.apply(null, contain); 
     console.log(max)
   }
}
5
  • Can you just sort by performance, then using find? Commented Oct 9, 2019 at 19:01
  • Working on it... Commented Oct 9, 2019 at 19:02
  • close voter, please provide the reason... Commented Oct 9, 2019 at 19:03
  • You would not need to store the performances in the contain array. You should store only the last object (or its index) in a variable, then update that whenever you find a matching one with better performance than the last. Then log its id afterwards. Commented Oct 9, 2019 at 19:04
  • Are you going to search for phrase multiple times? If yes, you could create an object or a Map with each unique phrase as key and the id of the lowest performance as it's value. Now you could get the value with just mappingObject[phrase] Commented Oct 9, 2019 at 19:16

5 Answers 5

2

I would go with :

const slides = [
{ id: 1, performance: 20, guided_phrases: ["I was sent", "I don't know"] },
{ id: 2, performance: 30, guided_phrases: ["to earth"] },
{ id: 3, performance: 40, guided_phrases: ["to protect you"] },
{ id: 4, performance: 15, guided_phrases: ["I was sent"] },
{ id: 5, performance: 50, guided_phrases: ["I was sent"] }
];

const logID = (phrase) => {
  console.log(slides.filter(x => x.performance === Math.min(...slides.filter(slide => slide.guided_phrases.includes(phrase)).map(x => x.performance)))[0].id);
}

logID('I was sent');

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

Comments

2

Here is a possible solution using some ES6:

var slides = [
{ id: 1, performance: 20, guided_phrases: ["I was sent", "I don't know"] },
{ id: 2, performance: 30, guided_phrases: ["to earth"] },
{ id: 3, performance: 40, guided_phrases: ["to protect you"] },
{ id: 4, performance: 15, guided_phrases: ["I was sent"] },
{ id: 5, performance: 50, guided_phrases: ["I was sent"] }
];

let phrase = "I was sent";

const findLowestId = (arr) => slides.filter(x => {
   const reg = new RegExp(phrase)
   return x.guided_phrases.find(y=> y.match(reg)) 
}).sort((a,b)=> a.performance > b.performance)[0].id

console.log(findLowestId(slides))

Comments

2

I used only one loop.

var slides = [
{ id: 1, performance: 20, guided_phrases: ["I was sent", "I don't know"] },
{ id: 2, performance: 30, guided_phrases: ["to earth"] },
{ id: 3, performance: 40, guided_phrases: ["to protect you"] },
{ id: 4, performance: 15, guided_phrases: ["I was sent"] },
{ id: 5, performance: 50, guided_phrases: ["I was sent"] }
];

const search = 'I was sent';

let result = -1;
let performance = 0;
slides.forEach((slide, index) => {
  if(slide.guided_phrases.includes(search) > -1 && performance < slide.performance) {
    performance = slide.performance;
    result = index;
  }
});

console.log('Result:', result);

Comments

2

This is how I would do it if you want a function that's readable and easy to follow:

const lowestPerformance = (slides, phrase) => {
    let currLowest = null;

    for (let i = 0; i < slides.length; i++) {
        let slide = slides[i];

        if (slide.guided_phrases.includes(phrase)) {
            currLowest =  ((currLowest === null) || slide.performance < (currLowest.performance)) ? slide : currLowest;
        }
    }

    return currLowest;
}

Comments

1
let phrase = "I was sent";
let contain = null;
let id = null;
for(let i = 0; i < slides.length; i++){

   if(slides[i].guided_phrases.includes(phrase)){
     if (contain == null) {
       contain = slides[i].performance;
       id = slides[i].id;
     } else {
       if (contain > slides[i].performance) {
         contain = slides[i].performance;
         id = slides[i].id;
       }
     }
  }
}
console.log(id);

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.