0

I have a array of objects getting from api like this

[0: {id: 1, name: "dine", restaurant: 2} 1: {id: 2, name: "pick", restaurant: 2}]

Now I want to show this just name of these both objects like this dine,pick . I was able to show this in a div like this

 {articleopt.ingredient.map(ingred => (
                                <div>{ingred.name + ","}</div>
                              ))}

But how can I assign this to a form input field value= {} ? My input field code is

<div className='col-sm-6'>
                <div className='form-group text-left'>
                  <label htmlFor='' className='small'>
                    Ingredient:
                  </label>
                  <InputField 
                  name='ingredient'
                  value={""}
                  placeholder={"Enter Ingredient comma separated"} />
                </div>
              </div>

1 Answer 1

1

Assuming you have some data like this, you can map/join the array down to a comma-separated string.

const data = [{id: 1, name: "dine", restaurant: 2}, {id: 2, name: "pick", restaurant: 2}];
const combined = data.map(el => el.name).join(",");

You can render JSX as follows:

<div className='form-group text-left'>
  <InputField value={combined} />
</div>
Sign up to request clarification or add additional context in comments.

5 Comments

thanks sir for answer .your solution is creating two fields , I want this data in one field with comma separated
Oh I see, so you just want one field ever?
Yes Exactly . One field with comma separated names
Updated! Let me know if this is what you're looking for
got it . thanks <3 is there any resources to read for issues like this ? kindly refer if any you have

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.