1

好的,所以我正在尝试利用来自 github api 的数据来制作随机表情符号“情绪环”类型的东西。我注意到他们的 api 中有一个用于表情符号的图像列表。我能够从 api 中获取数据并将数据字符串化。现在我正在尝试将 http 链接过滤到一个新数组中,因为他们的数据库具有按表情符号名称(例如心脏)分类的链接,所以没有简单的方法来过滤它们(我也是编码新手,所以也许身份证:o)。之前它给了我错误,例如 str 未定义,或者 endsWith 不是函数,但现在没有错误,它甚至不会显示我的 console.log(result)。好吧,无论如何,这是我的代码..非常感谢您抽出宝贵时间阅读本文!!!!

import React, { Component } from 'react';
import BrowserRouter from 'react-router-dom'
class InfoPage extends Component {

constructor(props) {
super(props);
 
this.state = {
newInfo: [],
info: [],
str: [],
};
}

componentDidMount() {
  this.getInfo();
  this.getEmoji();
}

//getting info from github api

getInfo = () => {

  fetch(`https://api.github.com/emojis`)
  .then((response) => {
    if(response.status === 200) {
      return response.json()
    }
    console.log('success')
  })
  .then((info) => {
    this.setState([{newInfo: info}])
   
    const str = JSON.stringify(info);
    this.setState({parsed: str})
    console.log("info",info)
  })

}

//if statement starts with htttp and ends with pngv8(meaning it's a link to a photo), push into array

getEmoji = () => {
  let result=[]
  let str = this.state.str 
  for (var i = 0; i < str.length; i++) {
  if (str.startsWith("http") && str.endsWith('v8')) {
  result.push(str)
  console.log(result)
  } else {
    console.log("not a url")
  }
  }
}


//get array of only http links -- make sure the links works

//use math.random and attatch it to the button to reveal a random photo onClick


render() {

return (

      <div className="main">

      Which Emoji Are You?

      <br></br>
      <button type="button" >Click Here To Find Out!</button>
      </div>




  )

  }

}

export default InfoPage;

4

0 回答 0