简体   繁体   English

从JSON数组React中删除括号

[英]Remove brackets from JSON array React

I am trying to remove the } { brackets from my outputted array in the browser, to make it look cleaner on my page, however I am unsure how to do so. 我正在尝试从浏览器中的输出数组中删除} {括号,以使其在页面上看起来更干净,但是我不确定如何这样做。 I have the array already outputting correctly, on a new line per object which is correct, but I'd like to remove the brackets. 我已经正确输出了数组,在每个对象的新行上是正确的,但是我想删除括号。

import React, { Component } from 'react';
import './App.css';
import $ from 'jquery';
import { Image } from 'react-native';

class App extends Component {

  state = {result : null}

  componentDidMount = () => {

    $.ajax({
      type: "GET",
      url: 'http://localhost:3001/data',
      data: {},
      xhrFields: {
        withCredentials: false
      },
      crossDomain: true,
      dataType: 'JSON',
      success: (result) => {
        this.setState({result : result});
      }
    });

  };

  render() {
    return (
          <div className="App">
            <header>
              <Image
              style={{width: 200, height: 50, margin: 'auto'}}
              source={require('./img/XXX.png')}
              />
            </header>
            <div className='renderhere'>
              <pre>{JSON.stringify(this.state.result, null, 2).replace(/]|[[]/g, '')}</pre>
            </div>
            <footer>Last Application Deployment v1.0. By XXX.</footer>
          </div>

        );
  }
    }

export default App;

The output currently looks like this: 当前的输出如下所示:

  {
    "appName": "App 1",
    "latestDeploymentDate": 0
  },
  {
    "appName": "App 2",
    "latestDeploymentDate": 1
  },
  {
    "appName": "App 3",
    "latestDeploymentDate": 2
  },
  {
    "App 4",
    "latestDeploymentDate": 3
  },
  {
    "appName": "App 5",
    "latestDeploymentDate": 4
  }

However I would like the output to look like this: 但是我希望输出看起来像这样:

    "appName": "App 1",
    "latestDeploymentDate": 0

    "appName": "App 2",
    "latestDeploymentDate": 1

    "appName": "App 3",
    "latestDeploymentDate": 2

    "appName": "App 4",
    "latestDeploymentDate": 3

    "appName": "App 5",
    "latestDeploymentDate": 4

And if it is possible i would also like to remove the speech marks, but just removing the brackets is fine for now. 而且,如果有可能,我也想删除语音标记,但是现在就删除括号就可以了。

You should not probably do it that way. 您可能不应该那样做。 Reasons: 原因:

  1. It's non natural way 这是非自然的方式

  2. Operations with strings (stringifying to json and replacing with regexp) is expensive. 使用字符串(字符串化为json并用正则表达式替换)的操作非常昂贵。

Instead you can map over your array: 相反,您可以映射数组:

<pre>
  {this.state.result.map(item => {
    return (
      <span>"appName": "{item.appName}"</span>
      <span>"latestDeploymentDate": "{item.latestDeploymentDate}"</span>
    )
  })
</pre>

This is not that valid from HTML spec's point of view and may probably broke your styling, but I've decided to leave it here for the case you might consider it useful. 从HTML规范的角度来看,这不是有效的方法,并且可能会破坏您的样式,但是出于您可能认为有用的情况,我决定将其保留在此处。

This should help you. 这应该对您有帮助。 Just do a regex replace! 只是做一个regex替换!

 var arr = [{ "appName": "App 1", "latestDeploymentDate": 0 }, { "appName": "App 2", "latestDeploymentDate": 1 }, { "appName": "App 3", "latestDeploymentDate": 2 }, { "appName": "App 4", "latestDeploymentDate": 3 }, { "appName": "App 5", "latestDeploymentDate": 4 }] var str = JSON.stringify(arr) var final = str.replace(/{|},|}/g, "\\n").replace(/\\[|\\]|"/g, "").replace(/,/g, ',\\n') console.log(final) 

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

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