简体   繁体   English

为对象中的每个键返回html

[英]return html for every key in object

I do apologize in advance I'm a JS/ReactJS newbie and I have had trouble phrasing the question, so if this is already answered somewhere I am sorry. 我确实要道歉,我是JS / ReactJS新手,我在表达问题时遇到了麻烦,因此,如果在某个地方已经回答了这个问题,对不起。

I have an object like this: Object {Joy: 0.719115, Extraversion: 0.59527, Agreeableness: 0.650457} 我有一个这样的对象: Object {Joy: 0.719115, Extraversion: 0.59527, Agreeableness: 0.650457}

I'd like to be able to return html for all of the keys in the object. 我希望能够为对象中的所有键返回html。 As of right now it only returns the html for the first key (obviously, as return breaks the loop, if I'm not mistaken). 截至目前,它仅返回第一个键的html(显然,如果没有记错的话,return会破坏循环)。 How do I achieve rendering of html of all the keys from the object? 如何实现对象中所有键的html呈现?

import React from 'react'

export const MessageSentiment = (props) => {
  var sentiment = JSON.parse(props.sentiment)
  console.log(sentiment)
    for(var key in sentiment ) {
      console.log(key, sentiment[key])
      return (<h1>{key}: {sentiment[key]*100}</h1>)
    }
}

These are the output and required output 这些是输出和必需的输出

Output:
<h1>Joy: 71.9115</h1>


Expected output:
<h1>Joy: 71.9115</h1>
<h1>Extraversion: 59.527</h1>
<h1>Agreeableness: 65.0456</h1>

Not sure if this has anything to do with it, but I get a warning in the console: 不知道这是否与它有关,但是我在控制台中收到警告:

../src/components/messages/MessageSentiment.js
  6:5  warning  The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype  guard-for-in

✖ 1 problem (0 errors, 1 warning)

printWarnings @ webpackHotDevClient.js:196
handleWarnings @ webpackHotDevClient.js:209
connection.onmessage @ webpackHotDevClient.js:255
EventTarget.dispatchEvent @ eventtarget.js:49
(anonymous) @ main.js:274
SockJS._transportMessage @ main.js:272
EventEmitter.emit @ emitter.js:44
WebSocketTransport.ws.onmessage @ websocket.js:35

Two things here. 这里有两件事。

  1. You need to always return one single element, in this case a div. 您需要始终返回一个元素,在这种情况下为div。 Inside of this element you can have whatever you want, but it has to be a single parent component. 在此元素内,您可以拥有所需的任何内容,但它必须是单个父组件。

  2. You will use map to iterate an array and get a new array. 您将使用map迭代一个数组并获得一个新数组。 In this case the new array will contain the <h1/> elements. 在这种情况下,新数组将包含<h1/>元素。

``` ```

export const MessageSentiment = (props) => {
  const sentiments = JSON.parse(props.sentiment);
  const keys = Object.keys(sentiments);

  return (
    <div>
     { keys.map(key => (<h1 key={key}>{key}: {sentiments[key]*100}</h1>)) }
    </div>
  );    
}

``` ```

Regards 问候

A React component can't return multiple React elements. 一个React组件不能返回多个React元素。 You should wrap them inside <div> or any other container element. 您应该将它们包装在<div>或任何其他容器元素内。

export const MessageSentiment = (props) => {
  var sentiment = JSON.parse(props.sentiment)
  return (
    <div>
      {
        Object.keys(sentiment).map(key => (
          <h1 key={key}>{key}: {sentiment[key]*100}</h1>
        ))
      }
    </div>
  )
}

And remember: keys should be given to the elements inside the array to give the elements a stable identity. 并记住:应该为数组内的元素赋予 ,以赋予元素稳定的身份。

you need to collect all the HTML in the array and return it. 您需要收集数组中的所有HTML并将其返回。 you can do 2 way 你可以做两种方式

  1. using map - map return new array without modifying existing array 使用map-map返回新数组而不修改现有数组

.

export const MessageSentiment = (props) => {
              var sentiment = JSON.parse(props.sentiment)
              return (
                     <div>
                      {
                         Object.keys(sentiment).map((key, index) => <h1 key={index}> {key}:{sentiment[key]*100}</h1>)
                      }
                    </div>
                      )
              }
  1. Using array push method 使用数组推送方法

.

 export const MessageSentiment = (props) => {
     var sentiment = JSON.parse(props.sentiment)
     let itemList = [];
     for(var key in sentiment ) {
       itemList.push(<h1>{key}: {sentiment[key]*100}</h1>)
     }
     return (
             <div>{itemList}</div>
           )
     }

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

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