-3

I have the following problem, which I struggle to solve:

I have an array of objects, all of them have same properties but different values. I receive the value of property two and based on it to find the value of property one.

What I tried as approach is the following:

  • make copy of the array
  • compare if the value match
  • take the other value if it matches

But this seems quite ugly as solution.

Any other ideas?

const array =  [{
  one: 3,
  two: "some value"
}, {
  one: 7,
  two: "some other value"
}]
3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
pantofka
  • 13
  • 5

1 Answers1

1

Use arr.find method:

const seekingValue = 'some value'

const objWithNeededVal = array.find(item => item.two === seekingValue);

const neededVal = objWithNeededVal ? objWithNeededVal.one : "";
Daniyal Lukmanov
  • 1,149
  • 10
  • 21