简体   繁体   English

React:如何显示带有换行符的JSON键值对?

[英]React: How to display JSON key value pairs with line breaks?

I have the following component with a function called onClickBtn() that contains another function called textBuilder() which iterated through a JSON object filled with data from a form. 我有一个带有名为onClickBtn()的函数的以下组件,该组件包含另一个名为textBuilder()函数,该函数通过填充有来自表单数据的JSON对象进行迭代。 I am using an email service to programatically send an email when the submit button is clicked. 单击提交按钮时,我正在使用电子邮件服务以编程方式发送电子邮件。 The only issue is that my email's Body value ends up looking like this in the email: 唯一的问题是我的电子邮件的“ Body价值”最终在电子邮件中看起来像这样:

name: Dean age: 26 email: dean@aol.com styles: Color locations: Right Leg specificLocation: fasdfasdfa description: fdghjdghj budgets: $1000-$1500 days: Wednesday months: June  

Instead of: 代替:

name: Dean 

age: 26 

email: dean@aol.com 

styles: Color 

locations: Right Leg 

specificLocation: fasdfasdfa 

description: fdghjdghj 

budgets: $1000-$1500 

days: Wednesday 

months: June  

Below is my component. 以下是我的组件。 Can someone tell me what I am doing wrong? 有人可以告诉我我在做什么错吗?

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';

const useStyles = makeStyles(theme => ({
  button: {  margin: theme.spacing(1), },
  input: { display: 'none', },
}));

export default function ContainedButtons(props) {
  const classes = useStyles();  

  const onClickBtn = () => {

    const textBuilder = () => {
      var doc = "";
      for (const key in props.values) {
       doc += key + ": " + props.values[key] + "\n";      
      }
      return doc;
    } 

    window.Email.send({
      Host : "smtp.elasticemail.com",
      Username : "dfmmalaw@gmail.com",
      Password : "d2296492-2689-49f7-ae3d-584f1507d23a",
      To : 'dfmmalaw@gmail.com',
      From : "dfmmalaw@gmail.com",
      Subject : "This is the subject",
      Body : textBuilder()
  }).then(
    message => alert(message)
  );
  };

    return (
      <div>
        <Button variant="contained" color="primary" className={classes.button} disabled = {!props.isEnabled} type="submit" onClick={onClickBtn}>
          Submit
        </Button>
      </div>
    );
}

It's because your email client is rendering the content as HTML. 这是因为您的电子邮件客户端将内容呈现为HTML。 So newlines are just converted to spaces. 因此,换行符只是转换为空格。 Try it with a break element, like so: 尝试使用break元素,如下所示:

doc += key + ": " + props.values[key] + "<br/>";

在一个<div>显示对象数组中每个 object 的键值对,每个对象都有换行符</div><div id="text_translate"><p>我有一个对象数组,我需要在 div 或 h6 中显示每个 object 的键值对。 我想将文本格式化为:key:value(此处换行)key:value(此处换行)...到目前为止我的代码是:</p><pre> Index.html: &lt;div class="game-history"&gt;&lt;/div&gt; JS: const gameHistoryReadable = response.games.map(item =&gt; { let gamesTemp = '' for (const [key, value] of Object.entries(item)) { gamesTemp += key + ': ' + value + '\n' } return gamesTemp }) $('.game-history').text('') $('.game-history').text(gameHistoryReadable)</pre><p> 显示:单元格:,,,,,,,, over:false _id:602fa7c46bc4480017fb05fa 所有者:602589d0b3d544001780bb69 创建时间:2021-02-19T11:57:56.087Z 更新时间:2021-02-19T11:57:56.087Z __cell : ,,,,,,,, over: false _id: 602fa7f26bc4480017fb05fb 所有者: 602589d0b3d544001780bb69 创建时间: 2021-02-19T11:58:42.605Z 更新时间: 2021-02-19T11:58:42.605Z, 细胞: __v ,,,,,, over: false _id: 60300f8a0d2da000170123cd 所有者: 602589d0b3d544001780bb69 createdAt: 2021-02-19T19:20:42.898Z updatedAt: 2021-02-19T19:20:42.898Z __s,v: 0,,,, ,,, over: false _id: 603011220d2da000170123d9 所有者: 602589d0b3d544001780bb69 创建时间: 2021-02-19T19:27:30.222Z 更新时间: 2021-02-19T19:27:30.222Z ,,,,结束:假 _id:603011a60d2da000170123dd 所有者:602589d0b3d544001780bb69 创建时间:2021-02-19T19:29:42.202Z 更新时间:2021-02-19T19:29:42.202Z __v,,,,过度_id: 603011aa0d2da000170123de 所有者: 602589d0b3d544001780bb69 创建时间: 2021-02-19T19:29:46.074Z 更新时间: 2021-02-1 9T19:29:46.074Z __v: 0,cells: ,,,,,,,, over: false _id: 603012260d2da000170123df 所有者: 602589d0b3d544001780bb69 createdAt: 2021-02-19T19:31:50.484Z updatedAt: 202-319 :50.484Z __v: 0,cells: ,,,,,,, over: false _id: 603013d20d2da000170123e0 所有者: 602589d0b3d544001780bb69 createdAt: 2021-02-19T19:38:58.767Z updatedAt: 2021-02-</p></div> - In a <div> show the key value pairs for each object in an array of objects with line breaks for each

暂无
暂无

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

相关问题 删除嵌套的json元素并显示为单个键值对 - remove nested json elements and display as individual key value pairs 尝试使用 map 在反应组件中显示键/值对? - Trying to display key/value pairs in react component using map? 如何使用Angular显示JSON对象中的键/值对 - How to display key-value pairs from a JSON object using Angular 如何在 react js 的表格网格中显示键值对? - How can i display key value pairs in a table grid in react js.? 在 React 中使用不同的键/值对结构渲染 JSON - Rendering JSON in React with varying structure of the key/value pairs 在一个<div>显示对象数组中每个 object 的键值对,每个对象都有换行符</div><div id="text_translate"><p>我有一个对象数组,我需要在 div 或 h6 中显示每个 object 的键值对。 我想将文本格式化为:key:value(此处换行)key:value(此处换行)...到目前为止我的代码是:</p><pre> Index.html: &lt;div class="game-history"&gt;&lt;/div&gt; JS: const gameHistoryReadable = response.games.map(item =&gt; { let gamesTemp = '' for (const [key, value] of Object.entries(item)) { gamesTemp += key + ': ' + value + '\n' } return gamesTemp }) $('.game-history').text('') $('.game-history').text(gameHistoryReadable)</pre><p> 显示:单元格:,,,,,,,, over:false _id:602fa7c46bc4480017fb05fa 所有者:602589d0b3d544001780bb69 创建时间:2021-02-19T11:57:56.087Z 更新时间:2021-02-19T11:57:56.087Z __cell : ,,,,,,,, over: false _id: 602fa7f26bc4480017fb05fb 所有者: 602589d0b3d544001780bb69 创建时间: 2021-02-19T11:58:42.605Z 更新时间: 2021-02-19T11:58:42.605Z, 细胞: __v ,,,,,, over: false _id: 60300f8a0d2da000170123cd 所有者: 602589d0b3d544001780bb69 createdAt: 2021-02-19T19:20:42.898Z updatedAt: 2021-02-19T19:20:42.898Z __s,v: 0,,,, ,,, over: false _id: 603011220d2da000170123d9 所有者: 602589d0b3d544001780bb69 创建时间: 2021-02-19T19:27:30.222Z 更新时间: 2021-02-19T19:27:30.222Z ,,,,结束:假 _id:603011a60d2da000170123dd 所有者:602589d0b3d544001780bb69 创建时间:2021-02-19T19:29:42.202Z 更新时间:2021-02-19T19:29:42.202Z __v,,,,过度_id: 603011aa0d2da000170123de 所有者: 602589d0b3d544001780bb69 创建时间: 2021-02-19T19:29:46.074Z 更新时间: 2021-02-1 9T19:29:46.074Z __v: 0,cells: ,,,,,,,, over: false _id: 603012260d2da000170123df 所有者: 602589d0b3d544001780bb69 createdAt: 2021-02-19T19:31:50.484Z updatedAt: 202-319 :50.484Z __v: 0,cells: ,,,,,,, over: false _id: 603013d20d2da000170123e0 所有者: 602589d0b3d544001780bb69 createdAt: 2021-02-19T19:38:58.767Z updatedAt: 2021-02-</p></div> - In a <div> show the key value pairs for each object in an array of objects with line breaks for each 如何以键值对形式在 React 中显示嵌套的 json 数据? - How to display nested json data in React in key and value pair form? 使用JSON的键值对 - Key value pairs using JSON 将键/值对转换为 json - Convert Key/Value pairs to json 如何在表中显示嵌套的键和值对 - how too display nested key and value pairs in table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM