0

I have a function that loops through an Object's keys until the key matches a keyword within the input. After finding a match, how can I then loop through the matched Object keys values ?

let obj = {
  music: {
    "do you like music": "Yes I do",
    "what music do you like": "Jazz"
  },

  sports: {
    "Do you like sports": "Yes"
  }
};

let input = "i like music";

function run(input) {
  let newStr = input.split(' ');
  let key = Object.keys(obj);
  key.forEach((key) => {
    newStr.forEach((str) => {
      if (key === str) {
        console.log(key)
      }
    });
  });
};

run(input);

5
  • 1
    For future reference, that's what the "tidy" button on the Stack Snippet modal is for :) Commented Nov 24, 2018 at 17:39
  • what results do you expect? Commented Nov 24, 2018 at 17:39
  • Seems like arrays would make more sense for nested objects rather than using unique object keys Commented Nov 24, 2018 at 17:40
  • 1
    Why not if(str in obj) and then just log the str since it is the same as the key, no loops needed. Commented Nov 24, 2018 at 17:53
  • Thanks Patrick for the tip (: Also I used str in obj and it achieved what I was looking for Commented Nov 25, 2018 at 5:41

1 Answer 1

1

You can use Array.flatMap and return matching word value from obj

I assumed you would want to return entire value for matching key

let obj = {
  	music:{
    	"do you like music":"Yes I do",
      "what music do you like": "Jazz"
    },
    
    sports:{
    	"Do you like sports": "Yes"
    }
};

function run(i) {
  return i.split(' ').flatMap(d => obj[d] || [])[0]
}

console.log(run("i like music"))

console.log(run("i like sports"))

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

12 Comments

Should also note that flatMap does not have broad browser support and would require polyfill
@charlietfl That’s correct. If you don’t mind me asking one thing.. i am developing myself technically and learning from you all by viewing your answers.. and understanding those. flatMap is also one of those features i learnt from SO but when i dont use it I get comment that i should use that and when I use it I get comment its not much supported.. pls can you advice if I should use it or dont prefer it Or shall always mention that it needs polyfill?. 😊 Thank you.
It does get tricky these days though so i understand the confusion. If developing only in node and not front end feature support isn't as much of an issue. ES6 features for front end where not using transpiler or for methods that aren't supported in legacy browsers does get complicated
Yes, but you can simply add a filter before map to only consider keys that are in obj and that should solve the problem. The real purpose of flatMap is to flatter a 2d array after map is applied but in this case you don't have a 2d array at all.
I believe if you use node version > 8 then it won't be any problem to you. @JimCiaston
|

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.