Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从URL数组中获取内容_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 如何从URL数组中获取内容

Javascript 如何从URL数组中获取内容,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我正在尝试从多个提要获取数据并将其存储在一个数组中 网址: var URLS = [ "https://www.wired.com/feed/rss", "http://feeds.bbci.co.uk/news/rss.xml", "http://www.nasa.gov/rss/dyn/breaking_news.rss" ]; 我要填充的数组: constructor() { super() this.state = { data: []

我正在尝试从多个提要获取数据并将其存储在一个数组中

网址:

var URLS = [
  "https://www.wired.com/feed/rss",
  "http://feeds.bbci.co.uk/news/rss.xml",
  "http://www.nasa.gov/rss/dyn/breaking_news.rss"
];
我要填充的数组:

constructor() {
    super()
    this.state = {
      data: []
    }
  }
从提要中提取标题和描述的函数

extractData(text) {
    var doc = new DOMParser().parseFromString(text, 'text/xml');
    var items_array = [];
    var items = doc.getElementsByTagName('item');

    for (var i = 0; i < items.length; i++) {
      items_array.push({
        title: items[i].getElementsByTagName('title')[0].lastChild.data,
        description: items[i].getElementsByTagName('description')[0].lastChild.data,                  
      })
    }
    return items_array;
  }

问题是数据没有显示在列表中,但是如果我添加测试数据,如下面的代码,它显示得很好:
数据:[…prevState.data,{title:“t1”,description:“d1”},{title:“t2”,description:“d2”}]

逻辑似乎工作得很好,但是您没有正确使用扩展运算符:

this.setState(
  (prevState, props) => ({
    data: [...prevState.data, ...this.extractData(responseData)]
  })
)
const data=[1,2,3,4]
常量外部元素=[5,6,7,8]
常量结果=[…数据,…外部元素]

console.log(result)
逻辑似乎工作正常,但是您没有正确使用扩展运算符:

this.setState(
  (prevState, props) => ({
    data: [...prevState.data, ...this.extractData(responseData)]
  })
)
const data=[1,2,3,4]
常量外部元素=[5,6,7,8]
常量结果=[…数据,…外部元素]
console.log(结果)