-2

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.

  • 1
    Do you mean like `text.push(ard[input])`? What result do you hope to see at the end? – Sebastian Simon Nov 19 '22 at 22:26
  • @SebastianSimon I need to get the text that matches from the object and assign it to text, Doesn't work for me `text.push(ard[input])` – user20550867 Nov 19 '22 at 22:28
  • Get familiar with [how to access and process objects, arrays, or JSON](/q/11922383/4642212), how to [access properties](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_Accessors), and how to create [objects](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer) or [arrays](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/Array#array_literal_notation). Then, please [edit] your post and show what `text` should be at the end, ideally as a literal. – Sebastian Simon Nov 19 '22 at 22:30
  • @SebastianSimon, I read this article three times, but it does not contain what I need I need to get a match if it (three) get the text from it, there I did not find what I need – user20550867 Nov 19 '22 at 22:33
  • What is “a match”, precisely? Again, please [edit] your post and show the desired final result of `text`. We have no idea, what exactly you’re trying to do here. – Sebastian Simon Nov 19 '22 at 22:44

1 Answers1

0

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']
Yasio
  • 450
  • 4
  • 6