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)
}
}
sortby performance, then usingfind?containarray. 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.phrasemultiple times? If yes, you could create an object or aMapwith each unique phrase as key and theidof the lowestperformanceas it's value. Now you could get the value with justmappingObject[phrase]