I want to pick the value of the given key and I did this using a ugly code: (I used two for loops to do a simple task)
const phrases = [
{0: "i was sent", 1: "have you ever"},
{2: "look", 3: "looked", 4: "looked", 5: "at", 6: "at"},
{7: "someone", 8: "somebody", 9: "to earth", 10: "sam"},
{11: "to protect"},
{12: "us", 13: "to earth"},
{14: "us", 15: "you"}
];
const result = getSpanIDText(8); // get value of 8 im the above array
console.log(result);
function getSpanIDText(spanID) {
for (let i = 0; i < phrases.length; i++) {
const set = phrases[i]; // each object inside phrases array
for (const [key, value] of Object.entries(set)) {
if (parseInt(key) === spanID) {
return value;
}
}
}
}
I wonder if there is a more clear code not need two for loops to achive the same result?