如何将值与数组进行比较

时间:2018-04-12 02:44:56

标签: javascript arrays json

这是我的JSON对象,我需要检查特定的 id 并获取日期

r2 <- raster(nrows =1, ncols =2)
r2[] <- c(1, 2)
levelplot(r2, margin = FALSE)

2 个答案:

答案 0 :(得分:0)

  • 您可以使用函数find根据条件获取具体内容。

此替代方法仅使用逻辑运算符||来避免对未定义值的访问错误。

&#13;
&#13;
var array = [{
    "id": "9b3b835b",
    "date": "Tue Mar 27 10:23:18 UTC 2018"
  },
  {
    "id": "57eab193",
    "date": "Thu Mar 29 14:45:23 UTC 2018"
  },
  {
    "id": "440f0cd9",
    "date": "Thu Mar 29 15:12:00 UTC 2018"
  },
];

var date = (array.find(o => o.id === "440f0cd9") || {}).date;
console.log(date);

date = (array.find(o => o.id === "ele") || {}).date;
console.log(date);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

yourVariable = [{
    "id": "9b3b835b",
    "date": "Tue Mar 27 10:23:18 UTC 2018"
  },
  {
    "id": "57eab193",
    "date": "Thu Mar 29 14:45:23 UTC 2018"
  },
  {
    "id": "440f0cd9",
    "date": "Thu Mar 29 15:12:00 UTC 2018"
  },
];

for (keys in yourVariable) {
  // supposing id your are looking for is 57eab193
  if (yourVariable[keys].id == '57eab193') {
    console.log(yourVariable[keys].date)
  }
}