0

I have an Object like this:

enter image description here

And an object like this:

const subjectQuantities = {
      9: 2,
      11: 1,
    };

Where 9 and 11 are similar to the object id of first Object what I want to do is set the value of an input field dynamically so what I did is this:

{ Object.keys(subjects).map((item, i) => (
   <li className="travelcompany-input clearfix" key={i}>
      <span className="input-label">{ subjects[item].name }</span>
         <div className="input-group">
           { subjects[item].id in subjectQuantities ?
            <input className="form-control form-travelcompany-input" type="text" value=*DYNAMIC VALUE* min="0" max="10" />
             : <p>TEST</p>
             }
         </div>
   </li>
))} 

How do I set the value of this input field dynamically based on the value of the subjectQuantities object.

1
  • maybe you want to use a defaultValue prop in the input element? Commented Nov 25, 2016 at 12:53

1 Answer 1

1

If i understand your question correctly you just need to do {subjectQuantities[item]}

See your example below.

{ Object.keys(subjects).map((item, i) => (
   <li className="travelcompany-input clearfix" key={i}>
      <span className="input-label">{ subjects[item].name }</span>
         <div className="input-group">
           { subjects[item].id in subjectQuantities ?
            <input className="form-control form-travelcompany-input" type="text" value={subjectQuantities[item]} min="0" max="10" />
             : <p>TEST</p>
             }
         </div>
   </li>
))} 
Sign up to request clarification or add additional context in comments.

2 Comments

I did it like this: value={this.subjectQuentaties[item]} why is it without this?
value={subjectQuantities[subjects[item].id]}

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.