2

I put in HTML

console.log(foo["_pristineData"]);

at console I see

enter image description here

value field is unique. I can get value filed by

document.getElementById("blablaInput").value;

it return BAR .

How to get text Bar join stock company based on BAR (what I can get)?

(Return a string, not an object like this question Get JavaScript object from array of objects by value of property The question is not duplicate)

Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • Its in `foo["_pristineData"][1].text` are you asking how to find the index, as thats 100% a dupe. – Lawrence Cherone Jun 22 '19 at 02:59
  • 1
    You mean you want to get value based on input value ? – Code Maniac Jun 22 '19 at 02:59
  • @LawrenceCherone I don't know how to get index what base-on `BAR`. I can get `BAR` right now, but I don't know how to get its index. Seemly, When I get index success, I can get its text. – Vy Do Jun 22 '19 at 03:00
  • @CodeManiac yes, I need return "Bar joint stock company" based-on `BAR` (field `value` is unique, it means 2 or many values are never duplicate. – Vy Do Jun 22 '19 at 03:01
  • Duplicate of [Get JavaScript object from array of objects by value of property](https://stackoverflow.com/questions/13964155/get-javascript-object-from-array-of-objects-by-value-of-property) – Lawrence Cherone Jun 22 '19 at 03:07
  • @DoNhuVy you can simply use `find` in this case as you have unique values, you don't need to find index explicitly – Code Maniac Jun 22 '19 at 03:20

2 Answers2

2

since you're field values are unique so You can use find and return values accordingly, in case you have multiple value and you want to capture all of them you can use filter

const foo =[
  {value: 'FOO', text:'some text'},
  {value: 'BAR', text: 'Some bar text'}
]

const getValue = () =>{
  let value = document.getElementById('id_1').value
  let found = foo.find(v=> v.value === value)
  let final = found ? found.text : 'Not found'
  console.log(final)
}
<input id='id_1' value=''></input>
<button onClick='getValue()'>Give me value</button>
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
1

Use find:

const foo = {
  _pristineIndex: [{value: "FOO", text: "Foo limited liability company"},
{value: "BAR", text: "Bar joint stock company"},
{value: "", text: ""}]
};

const { text: res } = foo._pristineIndex.find(({ value }) => value == "BAR");

console.log(res);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79