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)

6
  • Its in foo["_pristineData"][1].text are you asking how to find the index, as thats 100% a dupe. Commented Jun 22, 2019 at 2:59
  • 1
    You mean you want to get value based on input value ? Commented Jun 22, 2019 at 2: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. Commented Jun 22, 2019 at 3: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. Commented Jun 22, 2019 at 3:01
  • Duplicate of Get JavaScript object from array of objects by value of property Commented Jun 22, 2019 at 3:07

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

Comments

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);

4 Comments

It is quite near with my purpose. But I catch error Uncaught TypeError: Cannot read property 'find' of undefined
_pristineIndex is what I don't know how to get, or I cannot get.
Try bracket notation: foo["_pristineIndex"].
I also try const { text: res } = foo["_pristineIndex"].find(({ value }) => value == "BAR"); imgur.com/a/rp596Jj

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.