简体   繁体   English

如何在反应中使用嵌套循环

[英]How to use nested loop in react

I want to convert this code into react.js, I got an error when I used the map array inside the input tag.我想将此代码转换为 react.js,当我在 input 标签中使用 map 数组时出现错误。 ReactJS code is demonstrated below, ReactJS 代码如下所示,

This is my frontend code using laravel这是我使用 laravel 的前端代码

<div class="form-group">

<label for="tags" class="text-info">Select Tags</label>

@foreach($tags as $tag)
       <div class="checkbox">
             <label>
                 <input type="checkbox" 
                      value="{{$tag->id}}"
                      name="tags[]"
                 @foreach($post->tags as $t)
                      @if($tag->id == $t->id)checked 

                     @endif 
                 @endforeach >
                {{$tag->tag}}</label>
       </div>
        @endforeach
</div>

This is my react code... enter code here这是我的反应代码...在此处输入代码

   <div className="form-group row">
  <label for="inputEmail3" className="col-sm-2 col-form-label">
    TAG
  </label>
  <div className="col-sm-10">
    <div className="checkbox">
      {tate.tags.map(
        (tag) =>
        
          form.tags.map((taged) => (
                           
            <label>
              <input
                name="tag[]"
                type="checkbox"
                value={tag.id}
                onChange={handlecheck}

                defaultChecked={tag.id == taged.id ? true : false}
              />

             {tag.Tag_name}

            </label>
          )):</div></div></div>
render() {
  const elements = ['one', 'two', 'three'];

  const items = []

  for (const [index, value] of elements.entries()) {
    items.push(<li key={index}>{value}</li>)
  }

  return (
    <div>
      {items}
    </div>
  )
}

Another example would be like below:另一个例子如下:

render() {
    return (
      <table className="table">
        <tbody>
          {Object.keys(templates).map(function (template_name) {
            return (
              <tr key={template_name}>
                <tr>
                  <td>
                    <b>Template: {template_name}</b>
                  </td>
                </tr>
                {templates[template_name].items.map(function (item) {
                  return (
                    <tr key={item.id}>
                      <td>{item}</td>
                    </tr>
                  );
                })}
              </tr>
            );
          })}
        </tbody>
      </table>
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM