Get value of key based on another key

I have an array like:

users: [
        {
          name: "John",
          Volume: [80, 81, 83],
          Temp: [48, 46, 47],
          Weight: [110, 108, 103]
        },
        {
          name: "Amy",
          Volume: [90, 91, 93],
          Temp: [38, 36, 37],
          Weight: [110, 108, 103]
        },
        {
          name: "Elliot",
          Volume: [90, 91, 93],
          Temp: [38, 36, 37],
          Weight: [110, 108, 103]
        }
      ],

I have this.text = currentText and this.name = currentUser
currentText and currentUser are set dynamiclally, currentText can be either Volume, Temp or Weight.

how I can get data array based on user and text?
So if name was Amy and text Volume, I would get [90, 91, 93]
Something like:

users.filter(i => i.name === this.name).this.text

Thanks so much!

something like that could work, just remember that filter returns an array

1 Like

Thanks so much, that worked!

Sorry I’m actually getting undefined when I add in text at the end

yeah, you can’t use this.text like that, you need to use bracket notation there

1 Like

like users.filter(i => i.name === this.name).[this.text]? or like .this.text[0]

I also tried find(i => i.name === name)[0].this.text but got undefined

you need to combine both things

you first use bracket notation with a number to get the first element in the filtered array, which is an object, then use bracket notation to access the property of that object

1 Like

Thanks so much, got it by doing:

users.filter(i => i.name === name)[0][this.text]