Reactjs - 从另一个

时间:2016-12-09 07:18:56

标签: arrays json reactjs

我有一个

数组
var imagesArray = [
  {
   url: '4.jpg'
  },
  {
   url: '2.jpg'
  },
  {
   url: '3.jpg'
  }
];

我收到了

的JSON回复
   [{"path":"site:1.jpg","data":[]},
   {"path":"site:5.jpg","data":[]},
   {"path":"site:7.jpg","data":[]}]

如何将url中的imagesArray值替换为JSON响应的path值为1.jpg,5.jpg,7.jpg?

3 个答案:

答案 0 :(得分:2)

这将为您提供一个包含所需url属性的三个对象的数组。

class FV_Cell: UICollectionViewCell
 {
     override func awakeFromNib()
     {
         super.awakeFromNib()
           // Initialization code
    }
}

答案 1 :(得分:1)

也许你可以这样做:



let imagesArray = [
  {
    url: '4.jpg'
  },
  {
    url: '2.jpg'
  },
  {
    url: '3.jpg'
  }
];
    
let response = [
  {"path":"site:1.jpg","data":[]},
  {"path":"site:5.jpg","data":[]},
  {"path":"site:7.jpg","data":[]}
]

let newArray = imagesArray.map( (img, i) => img.url = response.filter( r => r.path)[i]);

console.log(JSON.stringify(newArray));




Here is fiddle.

答案 2 :(得分:1)

var imagesArray = [
  {
   url: '4.jpg'
  },
  {
   url: '2.jpg'
  },
  {
   url: '3.jpg'
  }
];

response =    [{"path":"site:1.jpg","data":[]},
   {"path":"site:5.jpg","data":[]},
   {"path":"site:7.jpg","data":[]}]

class Test extends React.Component {
        render(){
    imagesArray = imagesArray.map((img,i)=>{return({url : response[i].path}) })
        return (
        <code>{JSON.stringify(imagesArray)}</code>
      )
    }
}

React.render(<Test />, document.getElementById('container'));

DEMO