Получить ключи каждого объекта и вставить эти данные в массив - PullRequest
0 голосов
/ 18 марта 2020

Я хочу получить ключи каждого объекта кадра и вставить эти данные в массив. После него должны быть массивы в массиве. Я пробовал несколько способов и не понял этого. Любые предложения?
Это вывод. json, с которым я буду работать, он может go до 550 номеров кадров.

[{"frame_number": 1, "roi0": [101.78202823559488, 99.39509279584912, 49.546951219239915, 29.728170731543948], "intensity0": 80.0, "roi1": [101.78202823559488, 99.39509279584912, 49.546951219239915, 29.728170731543948], "intensity1": 157.0},
{"frame_number": 2, "roi0": [102.56623228630755, 97.95906005049548, 50.25603182631066, 30.153619095786393], "intensity0": 80.0, "roi1": [102.56623228630755, 97.95906005049548, 50.25603182631066, 30.153619095786393], "intensity1": 158.0},
{"frame_number": 3, "roi0": [103.39336535376313, 98.20468223716023, 49.58465295946593, 29.750791775679556], "intensity0": 80.0, "roi1": [103.39336535376313, 98.20468223716023, 49.58465295946593, 29.750791775679556], "intensity1": 157.0},

Вот мое приложение. js где я получаю выходной файл. json из API и отправляю его компоненту, button_footer

import "bootstrap/dist/css/bootstrap.css";
import React from "react";
import Radio_Button from "./components/Radio_Button.js";
import Buttons_Footer from "./components/Buttons_Footer.js";
import LeftPane from "./components/LeftPane.js";

//import './App.css';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { apiResponse: [] };

  }
  // Comunicate with API
  callAPI() {
    fetch("http://localhost:9000/IntensityAPI") //React app talks to API at this url
      .then(res => res.json())
      .then(res => this.setState({ apiResponse: res }));
  }
  componentWillMount() {
    this.callAPI();
  }
  render() {
    return (

      <div className="App">

        <header className="App-header">
          <p></p>
          <div class="row fixed-bottom no-gutters">
            <div class="col-3 fixed-top fixed-bottom">

              <LeftPane></LeftPane>
            </div>
            <div class="offset-md-3" >
            <Buttons_Footer readings =   {this.state.apiResponse}/>
            </div>
          </div>
        </header>
      </div>
    );
  }
}
export default App;

Ниже приводится button_footer, где я пытался обработать эти данные и поместить его в массив, но не удалось.

import $ from "jquery";
import React, { Component } from 'react';
import { MDBFormInline } from 'mdbreact';
import { Container, Row, Col } from 'reactstrap';
import Radio_Button from "./Radio_Button.js";
// Footer Component with checkbox's used to select region/region's of interest 
class Buttons_Footer extends Component {
  // Enables the Functionality of the "Select Multiple Region's" switch using jquerys
  componentDidMount() {
    $(".region").click(function(e){
    if($('#customSwitches').is(':not(:checked)')){
      if($('.region:checked').length > 1){ // Multiply regions unable to be selected
        alert('You can not check multiple');
        e.preventDefault();
      }
    }
});

$("#customSwitches").click(function(e){ // Multiply regions able to be selected
   $(".region").prop('checked', false);
}); }

//<p>{this.props.region.roi0}</p>
  render() {
    return (
      <Container class = "container  offset-md-3" > 
            <div className='custom-control custom-switch' >
            <input type='checkbox' className='custom-control-input' id='customSwitches' />
            <label  className='custom-control-label' htmlFor='customSwitches'>
                 Select Multiple Region's
            </label> 
            {this.props.readings.map((region)=>{
              return <Radio_Button region ={region} key ={region.frame_number}/>
            })}
            Object.keys({this.props.readings}).map((key, index) => {
  const myItem = myObject[key]
  return <MyComponent myItem={myItem} key={index} />
})
              <MDBFormInline>
                <input class="region" type="checkbox" name="region1" value="1" />
                <label for="region1"> 1</label>
                <input class="region" type="checkbox" name="region2" value="2" />
                <label for="region2"> 2</label>
             </MDBFormInline>
            </div>      
    </Container>
    );
  }
}

export default Buttons_Footer;

```

1 Ответ

0 голосов
/ 18 марта 2020

Может быть, эта работа

this.props.readings.map((reading, index) => {
   Object.keys(reading).map((key, index) => {
       const myItem = reading[key]
      return <MyComponent myItem={myItem} key={index} />
   })
})
...