如何使用具有相同键的许多对象更新数组中元素的值?

时间:2017-09-13 14:17:30

标签: javascript arrays json react-native

我正在开发一个本机应用程序,我有一个目前有静态数据的数据库,数据在文件entry.js中的数组中, 我想让carousal充满活力, 我想在HomeScreen.js文件中解析来自API的json数据之后,更新数组中每个对象的标题,副标题,插图的值。

Entry.js

export const ENTRIES1 = [
{
    title: 'Beautiful and dramatic Antelope Canyon',
    subtitle: 'Lorem ipsum dolor sit amet et nuncat mergitur',
    illustration: 'http://i.imgur.com/UYiroysl.jpg'
},
{
    title: 'Earlier this morning, NYC',
    subtitle: 'Lorem ipsum dolor sit amet',
    illustration: 'http://i.imgur.com/UPrs1EWl.jpg'
},
{
    title: 'White Pocket Sunset',
    subtitle: 'Lorem ipsum dolor sit amet et nuncat ',
    illustration: 'http://i.imgur.com/MABUbpDl.jpg'
},
{
    title: 'Acrocorinth, Greece',
    subtitle: 'Lorem ipsum dolor sit amet et nuncat mergitur',
    illustration: 'http://i.imgur.com/KZsmUi2l.jpg'
},
{
    title: 'The lone tree, majestic landscape of New Zealand',
    subtitle: 'Lorem ipsum dolor sit amet',
    illustration: 'http://i.imgur.com/2nCt3Sbl.jpg'
},
{
    title: 'Middle Earth, Germany',
    subtitle: 'Lorem ipsum dolor sit amet',
    illustration: 'http://i.imgur.com/lceHsT6l.jpg'
}];

这是我想要解析数据并更新数组的地方。

HomeScreen.js

import { ENTRIES1, ENTRIES2 } from './static/entries';
export default class HomeScreen extends React.Component {
componentDidMount() {
return fetch('http://api.com/api/get_posts')
  .then((response) => response.json())
  .then((responseJson) => {
    try {

    }   catch (error) {
        // Error saving data
    }           
    this.setState({


    }, function() {
      // do something with new state
    });
  })
  .catch((error) => {
    console.error(error);
  });
}
}

API结构如下:

{
 "status": "ok",
 "count": 8,
 "posts": [
  {
  "title": 76964,
  "subtitle": "post",
  "illustration": "modi-shinzo-abe-go-roadshow-gujarat"
  },
  {
  "title": 76964,
  "subtitle": "post",
  "illustration": "modi-shinzo-abe-go-roadshow-gujarat"
  },
  {
  "title": 76964,
  "subtitle": "post",
  "illustration": "modi-shinzo-abe-go-roadshow-gujarat"
  }
  ]
  }

我想在api中的posts数组在Entry.js文件的数组ENTRIES1中进行解析和更新。

0 个答案:

没有答案