0
my_arr = [
  { "keywords": "set,on,increease,decrease,low,medium,high,forward,backward", "commands": "SET" },
  { "keywords": "get,access,retrieve", "commands": "GET" },
  { "keywords": "run", "commands": "RUN" },
  { "keywords": "stop", "commands": "STOP" }
]

i want to find if increase exists in keywords?

pilchard
  • 12,414
  • 5
  • 11
  • 23
telus
  • 11
  • `my_arr.map(e => e.keywords.split(',').includes('increase'))` – AlexSp3 Apr 24 '22 at 12:52
  • `console.log(my_arr.some(f=>f.keywords.split(',').indexOf('increase')>-1))` – Royi Namir Apr 24 '22 at 12:52
  • or [Javascript-searching for a string in the properties of an array of objects](https://stackoverflow.com/questions/47653927/javascript-searching-for-a-string-in-the-properties-of-an-array-of-objects) – pilchard Apr 24 '22 at 12:53
  • hi this is fine how about if i find increase then i get the whole object as output something like this { "keywords": "set,on,increase,decrease,low,medium,high,forward,backward", "commands": "SET" } – telus Apr 24 '22 at 13:05
  • then [JavaScript: find objects in array with property containing a specific string](https://stackoverflow.com/questions/64470454/javascript-find-objects-in-array-with-property-containing-a-specific-string) or [Filter array of objects whose any properties contains a value](https://stackoverflow.com/questions/44312924/filter-array-of-objects-whose-any-properties-contains-a-value) – pilchard Apr 24 '22 at 13:31

2 Answers2

0

What about something like this

const data = [
  { "keywords": "set,on,increase,decrease,low,medium,high,forward,backward", "commands": "SET" },
  { "keywords": "get,access,retrieve", "commands": "GET" },
  { "keywords": "run", "commands": "RUN" },
  { "keywords": "stop", "commands": "STOP" }
]


const keywordExists = keyword => data.flatMap(({keywords}) => keywords.split(',')).includes(keyword)


console.log(keywordExists('increase'))
console.log(keywordExists('decrease'))
console.log(keywordExists('not valid keyword'))
R4ncid
  • 6,944
  • 1
  • 4
  • 18
  • hi this is fine how about if i find increase then i get the whole object as output something like this { "keywords": "set,on,increase,decrease,low,medium,high,forward,backward", "commands": "SET" } – telus Apr 24 '22 at 13:05
  • @telus you just have to replace `flatMap` with `find` – R4ncid Apr 24 '22 at 13:26
-1

I think this must be work.

const keys = [
  { "keywords": "set,on,increease,decrease,low,medium,high,forward,backward", "commands": "SET" },
  { "keywords": "get,access,retrieve", "commands": "GET" },
  { "keywords": "run", "commands": "RUN" },
  { "keywords": "stop", "commands": "STOP" }
]

function check(value) {
    // On every value on keys get index
    for (let i in keys) {
        // Get key from keys with index value
        const key = keys[i];
        // Check if is matching
        if (key.keywords.split(',').includes(value))
            // If successfuly matching is found return commands value
            return key.commands;
    }
    // If any result doesnt returned, return null.
    return null;
}
Wraith
  • 1
  • 1