首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用反应中的对象数组呈现组件

如何用反应中的对象数组呈现组件
EN

Stack Overflow用户
提问于 2018-03-05 13:52:34
回答 1查看 101关注 0票数 1

设置我的问题:我想对文本文档和文本区域进行排序。数据结构如下:

代码语言:javascript
复制
{"words": ["Horn ", "Cirque ", "Glacier in Longitudinal Section "] }
{"words": ["my other ", "line ", "of texts"] }

在一个数组里。必须使用webComponent对每个句子使用React表示结果:

代码语言:javascript
复制
textContent.map((sentences, index) =>
              <article>
               <desc-sentence>{sentences}</desc-sentence>
              </article>

          )

textContent是包含区域中每个句子的变量。

但是物体在反应中的儿童是无效的。一个数组是如何被命令的,如何用索引传递数组?

*编辑

输出应该是

代码语言:javascript
复制
let textContent = [
     {"words": ["Horn ", "Cirque ", "Glacier in Longitudinal Section "]},
     {"words": ["V-shaped valley ", "carved by a river "]}
]

console.log(textContent[0].words);
代码语言:javascript
复制
<section>
<!-- inside the textContent loop of words-->
<article>
  <p>{sentence}</p>  
  <p>{sentence}</p>
 </article>
 <article>
  <p>{sentence}</p>  
  <p>{sentence}</p>
 </article>
</section>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-06 05:27:22

下面是基于您的requirement.You的相同代码,可以检查HTML,我认为这主要是您想要的,标记中包含了所有的名称

代码语言:javascript
复制
import React from 'react'
class TestJS extends React.Component {
    render(){
        let textContent = [
            {"words": ["Horn ", "Cirque ", "Glacier in Longitudinal Section "]},
            {"words": ["V-shaped valley ", "carved by a river "]}
        ];
        
        let finalStringMessage = [];
        let textContentLength = textContent.length;
        for(let i=0; i < textContentLength; i++){
            let wordsArray = textContent[i].words;
            let articleName = wordsArray.map((sentences, index) =>
                    <desc-sentence>{sentences}</desc-sentence>

            );
            finalStringMessage.push(<article>{articleName}</article>)
        }
        return(
            <div>{finalStringMessage}</div>
        )
    }
}

export default TestJS;

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49111998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档