在ReactJS中检索JSON数组元素

时间:2016-07-21 04:45:40

标签: json

我在ReactJS中有以下json文件:

{
  "locations": [
      {
        "id": 8817,
        "loc": "NEW YORK CITY"
      },
      {
        "id": 2873,
        "loc": "UNITED STATES"
      },
      {
        "id": 1501
        "loc": "NEW YORK STATE"
      }
    ]
}

如何获取id = xxxx的元素的值?另外,当id = xxxx时,如何获得loc

2 个答案:

答案 0 :(得分:0)

你可以使用underscorejs

let array_of_ids = _.pluck(json_object.locations,"id")

//now find the index of your particular id

let index = _.indexOf(array_of_ids,yourId)

//now your required object is

let your_object = json_object.locations[index]

多数民众赞成

欢呼声

答案 1 :(得分:0)

您可以使用过滤功能。

let RightImageView = UIImageView()
RightImageView.image = image

let RightView = UIView()
RightView.addSubview(RightImageView)
RightView.frame = CGRectMake(0, 0, 30,30)
RightView.backgroundColor = UIColor.redColor()
RightImageView.backgroundColor = UIColor.blueColor()

RightImageView.contentMode = UIViewContentMode.ScaleAspectFill

RightImageView.frame = CGRectMake(0, 0,30,30)

textFieldForCountry.rightView = RightView