Home > Enterprise >  like return result object action
like return result object action

Time:11-20

I get the data: input
I need to find a match and assign it to a variable
How can I do that? ForEach dont work

console.log(input) // three
var ard = {
            one: `chekertest`,
            two: `chekertest`,
            three: `chekertest`,
            four: `chekertest`,
        }
        
        let text = []

        for (const input in ard) {
            // ???? like return result 
            
            text  = input
          }

I did it right, but it doesn't work for me for some reason.

CodePudding user response:

You didn't specify if you need an array of values, but I'm assuming you do. Does this solve your problem?

const input = 'three'
console.log(input)
var ard = {
            one: `chekertest`,
            two: `chekertest`,
            three: `chekertest`,
            four: `chekertest`,
        }
const results = Object.entries(ard).filter(a => a[0] === input).map(r => r[1])

console.log(results) // ['chekertest']

  • Related