1

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?

Vivek Jain
2,8726 gold badges14 silver badges29 bronze badges
asked Aug 26, 2020 at 15:44

5 Answers 5

1

This would eliminate the nested for loop, a little bit easier to read.

function getSpanIDText(spanID) {
 
 for(let phrase of phrases) {
 
 if(phrase[spanID]) {
 return phrase[spanID];
 }
 }
 }
answered Aug 26, 2020 at 15:52
Sign up to request clarification or add additional context in comments.

Comments

1

I guess you could do something like this, provided you're sure each object will contain unique number keys.

let item = phrases.map(obj => Object.entries(obj)).flat().filter(arr => arr[0] == spanId);
return item.length ? item[0][1] : false;

Gives

getSpanIDText(8); //"somebody"
answered Aug 26, 2020 at 15:50

Comments

1

I don't know if it's really better but the following should work as well

function getSpanId(phrases, spanId) {
 return new Map(phrases.map(Object.entries).flat()).get(spanId)
}
answered Aug 26, 2020 at 15:51

Comments

1

Try this.

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);
function getSpanIDText(id) {
 let output
 phrases.forEach(x => {
 return Object.keys(x).forEach(y => {
 if (+y === id) {
 output = x[y];
 }
 });
 });
 return output;
}
console.log(result);

answered Aug 26, 2020 at 15:57

Comments

1

You could use an optimized reduce function.

When the reduce function finds a matching key, it mutates the duplicated array to break out of the reduce loop.

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"}
];
function getSpan(phrases, key) {
 return phrases.slice(0).reduce((acc, cv, i, arr) => {
 if (key in cv) {
 arr = [];
 return cv[key];
 }
 return acc
 }, undefined)
}
console.log(getSpan(phrases, 8)) // "somebody"
console.log(getSpan(phrases, 22)) // undefined

answered Aug 26, 2020 at 15:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.