如何基于属性值筛选包含数组的对象

3

我正在尝试根据另一个属性的值从对象数组中获取一个值。

示例对象数组:

objArray = [ { foo: 1, bar: 2}, { foo: 3, bar: 4}, { foo: 5, bar: 6} ];

我试图通过过滤 "2" 来获取 "1"。 我不知道 "bar: 2" 的位置,而且由于数据每天都在变化,objArray 可能会在第二天看起来像这样:

objArray = [ { foo: 3, bar: 4}, { foo: 1, bar: 2}, { foo: 5, bar: 6} ];

我在考虑过滤:

但是我一无所获...


2
可以使用ES6的箭头函数进行简写:let objectWithFoo = objArray.filter(obj => obj.foo === 2); - ibrahim mahrir
@ibrahimmahrir 当我使用真实的JSON数据运行此函数时,为什么我的控制台会显示“objArray.filter不是一个函数”?是否有可能我有一个对象而不是对象数组? - J Doe
@JDoe,你的 objObj 看起来不对。 - ibrahim mahrir
2
@JDoe 仍然不正确。(没有键) - ibrahim mahrir
添加了我的对象。这个函数应该能用吗? - J Doe
显示剩余11条评论
3个回答

0
你需要使用 array.filter 而不是 strict equality

    var objArray = [ { foo: 3, bar: 4}, { foo: 1, bar: 2}, { foo: 5, bar: 6} ];

    var result = objArray.filter(function(e) {
      return e.bar == '2'
    })

    console.log(result)


2
字符串里面放数字?别这样。而且,你可以简化为 objArray.filter(e => e.bar === 2) - Sterling Archer
当我使用真实的JSON数据运行此函数时,为什么我的控制台会显示“objArray.filter不是一个函数”? - J Doe

0

这种方法可能不是最优雅的,但确实可行:

temp_at_15=[];
vals=Object.values(data);
vals.forEach(function(el) {
if(Array.isArray(el)) 
el.forEach(function(o) {
if(o.dt_txt.includes('15:00:00'))
 temp_at_15.push(o.main.temp);
});
});

console.log(temp_at_15);

演示:

data = {
  "cod": "200",
  "message": 0.0027,
  "cnt": 40,
  "list": [{
    "dt": 1512658800,
    "main": {
      "temp": 293.55,
      "temp_min": 283.891,
      "temp_max": 293.55,
      "pressure": 763.05,
      "sea_level": 1029.9,
      "grnd_level": 763.05,
      "humidity": 73,
      "temp_kf": 9.66
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.06,
      "deg": 198.002
    },
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-07 15:00:00"
  }, {
    "dt": 1512669600,
    "main": {
      "temp": 299.19,
      "temp_min": 291.944,
      "temp_max": 299.19,
      "pressure": 762.15,
      "sea_level": 1025.76,
      "grnd_level": 762.15,
      "humidity": 45,
      "temp_kf": 7.24
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.31,
      "deg": 199.005
    },
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-07 18:00:00"
  }, {
    "dt": 1512680400,
    "main": {
      "temp": 297.16,
      "temp_min": 292.327,
      "temp_max": 297.16,
      "pressure": 759.96,
      "sea_level": 1021.77,
      "grnd_level": 759.96,
      "humidity": 42,
      "temp_kf": 4.83
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "02d"
    }],
    "clouds": {
      "all": 8
    },
    "wind": {
      "speed": 1.35,
      "deg": 205.503
    },
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-07 21:00:00"
  }, {
    "dt": 1512691200,
    "main": {
      "temp": 290.34,
      "temp_min": 287.926,
      "temp_max": 290.34,
      "pressure": 760.22,
      "sea_level": 1023.05,
      "grnd_level": 760.22,
      "humidity": 57,
      "temp_kf": 2.41
    },
    "weather": [{
      "id": 801,
      "main": "Clouds",
      "description": "few clouds",
      "icon": "02n"
    }],
    "clouds": {
      "all": 20
    },
    "wind": {
      "speed": 1.1,
      "deg": 214.51
    },
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-08 00:00:00"
  }, {
    "dt": 1512702000,
    "main": {
      "temp": 279.389,
      "temp_min": 279.389,
      "temp_max": 279.389,
      "pressure": 761.16,
      "sea_level": 1026.01,
      "grnd_level": 761.16,
      "humidity": 89,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 0.96,
      "deg": 194.002
    },
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-08 03:00:00"
  }, {
    "dt": 1512712800,
    "main": {
      "temp": 275.883,
      "temp_min": 275.883,
      "temp_max": 275.883,
      "pressure": 761.04,
      "sea_level": 1027.29,
      "grnd_level": 761.04,
      "humidity": 83,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 0.87,
      "deg": 199.002
    },
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-08 06:00:00"
  }, {
    "dt": 1512723600,
    "main": {
      "temp": 274.217,
      "temp_min": 274.217,
      "temp_max": 274.217,
      "pressure": 760.17,
      "sea_level": 1027.31,
      "grnd_level": 760.17,
      "humidity": 83,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 0.82,
      "deg": 197
    },
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-08 09:00:00"
  }, {
    "dt": 1512734400,
    "main": {
      "temp": 273.823,
      "temp_min": 273.823,
      "temp_max": 273.823,
      "pressure": 759.84,
      "sea_level": 1028.26,
      "grnd_level": 759.84,
      "humidity": 89,
      "temp_kf": 0
    },
    "weather": [{
      "id": 500,
      "main": "Rain",
      "description": "light rain",
      "icon": "10n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 0.86,
      "deg": 191.004
    },
    "rain": {
      "3h": 0.02
    },
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-08 12:00:00"
  }, {
    "dt": 1512745200,
    "main": {
      "temp": 282.332,
      "temp_min": 282.332,
      "temp_max": 282.332,
      "pressure": 760.71,
      "sea_level": 1029.16,
      "grnd_level": 760.71,
      "humidity": 86,
      "temp_kf": 0
    },
    "weather": [{
      "id": 500,
      "main": "Rain",
      "description": "light rain",
      "icon": "10d"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.22,
      "deg": 193.004
    },
    "rain": {
      "3h": 0.02
    },
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-08 15:00:00"
  }, {
    "dt": 1512756000,
    "main": {
      "temp": 290.211,
      "temp_min": 290.211,
      "temp_max": 290.211,
      "pressure": 759.94,
      "sea_level": 1025.5,
      "grnd_level": 759.94,
      "humidity": 47,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.84,
      "deg": 198
    },
    "rain": {},
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-08 18:00:00"
  }, {
    "dt": 1512766800,
    "main": {
      "temp": 290.473,
      "temp_min": 290.473,
      "temp_max": 290.473,
      "pressure": 758.28,
      "sea_level": 1022.47,
      "grnd_level": 758.28,
      "humidity": 41,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 2.38,
      "deg": 202.501
    },
    "rain": {},
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-08 21:00:00"
  }, {
    "dt": 1512777600,
    "main": {
      "temp": 285.873,
      "temp_min": 285.873,
      "temp_max": 285.873,
      "pressure": 759.05,
      "sea_level": 1024.69,
      "grnd_level": 759.05,
      "humidity": 55,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.79,
      "deg": 202.509
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-09 00:00:00"
  }, {
    "dt": 1512788400,
    "main": {
      "temp": 278.749,
      "temp_min": 278.749,
      "temp_max": 278.749,
      "pressure": 760.62,
      "sea_level": 1028.57,
      "grnd_level": 760.62,
      "humidity": 85,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "02n"
    }],
    "clouds": {
      "all": 8
    },
    "wind": {
      "speed": 1.32,
      "deg": 193.002
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-09 03:00:00"
  }, {
    "dt": 1512799200,
    "main": {
      "temp": 276.029,
      "temp_min": 276.029,
      "temp_max": 276.029,
      "pressure": 760.99,
      "sea_level": 1030.3,
      "grnd_level": 760.99,
      "humidity": 82,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "02n"
    }],
    "clouds": {
      "all": 8
    },
    "wind": {
      "speed": 0.99,
      "deg": 197.501
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-09 06:00:00"
  }, {
    "dt": 1512810000,
    "main": {
      "temp": 273.727,
      "temp_min": 273.727,
      "temp_max": 273.727,
      "pressure": 760.62,
      "sea_level": 1030.77,
      "grnd_level": 760.62,
      "humidity": 82,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 0.86,
      "deg": 209.001
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-09 09:00:00"
  }, {
    "dt": 1512820800,
    "main": {
      "temp": 273.178,
      "temp_min": 273.178,
      "temp_max": 273.178,
      "pressure": 761.03,
      "sea_level": 1032.5,
      "grnd_level": 761.03,
      "humidity": 85,
      "temp_kf": 0
    },
    "weather": [{
      "id": 500,
      "main": "Rain",
      "description": "light rain",
      "icon": "10n"
    }],
    "clouds": {
      "all": 20
    },
    "wind": {
      "speed": 0.76,
      "deg": 257.502
    },
    "rain": {
      "3h": 0.015
    },
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-09 12:00:00"
  }, {
    "dt": 1512831600,
    "main": {
      "temp": 278.661,
      "temp_min": 278.661,
      "temp_max": 278.661,
      "pressure": 763.45,
      "sea_level": 1034.75,
      "grnd_level": 763.45,
      "humidity": 82,
      "temp_kf": 0
    },
    "weather": [{
      "id": 500,
      "main": "Rain",
      "description": "light rain",
      "icon": "10d"
    }],
    "clouds": {
      "all": 24
    },
    "wind": {
      "speed": 1.92,
      "deg": 359
    },
    "rain": {
      "3h": 0.08
    },
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-09 15:00:00"
  }, {
    "dt": 1512842400,
    "main": {
      "temp": 284.912,
      "temp_min": 284.912,
      "temp_max": 284.912,
      "pressure": 763.85,
      "sea_level": 1031.38,
      "grnd_level": 763.85,
      "humidity": 59,
      "temp_kf": 0
    },
    "weather": [{
      "id": 500,
      "main": "Rain",
      "description": "light rain",
      "icon": "10d"
    }],
    "clouds": {
      "all": 24
    },
    "wind": {
      "speed": 1.21,
      "deg": 3.00043
    },
    "rain": {
      "3h": 0.1
    },
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-09 18:00:00"
  }, {
    "dt": 1512853200,
    "main": {
      "temp": 287.77,
      "temp_min": 287.77,
      "temp_max": 287.77,
      "pressure": 762.52,
      "sea_level": 1028.1,
      "grnd_level": 762.52,
      "humidity": 44,
      "temp_kf": 0
    },
    "weather": [{
      "id": 500,
      "main": "Rain",
      "description": "light rain",
      "icon": "10d"
    }],
    "clouds": {
      "all": 48
    },
    "wind": {
      "speed": 1.2,
      "deg": 1.5011
    },
    "rain": {
      "3h": 0.015
    },
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-09 21:00:00"
  }, {
    "dt": 1512864000,
    "main": {
      "temp": 284.327,
      "temp_min": 284.327,
      "temp_max": 284.327,
      "pressure": 762.79,
      "sea_level": 1029.72,
      "grnd_level": 762.79,
      "humidity": 48,
      "temp_kf": 0
    },
    "weather": [{
      "id": 803,
      "main": "Clouds",
      "description": "broken clouds",
      "icon": "04n"
    }],
    "clouds": {
      "all": 56
    },
    "wind": {
      "speed": 0.71,
      "deg": 334.001
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-10 00:00:00"
  }, {
    "dt": 1512874800,
    "main": {
      "temp": 277.861,
      "temp_min": 277.861,
      "temp_max": 277.861,
      "pressure": 764.46,
      "sea_level": 1033.5,
      "grnd_level": 764.46,
      "humidity": 74,
      "temp_kf": 0
    },
    "weather": [{
      "id": 802,
      "main": "Clouds",
      "description": "scattered clouds",
      "icon": "03n"
    }],
    "clouds": {
      "all": 36
    },
    "wind": {
      "speed": 1.07,
      "deg": 353.501
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-10 03:00:00"
  }, {
    "dt": 1512885600,
    "main": {
      "temp": 274.729,
      "temp_min": 274.729,
      "temp_max": 274.729,
      "pressure": 764.78,
      "sea_level": 1035.66,
      "grnd_level": 764.78,
      "humidity": 84,
      "temp_kf": 0
    },
    "weather": [{
      "id": 500,
      "main": "Rain",
      "description": "light rain",
      "icon": "10n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.15,
      "deg": 7.50085
    },
    "rain": {
      "3h": 0.01
    },
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-10 06:00:00"
  }, {
    "dt": 1512896400,
    "main": {
      "temp": 272.021,
      "temp_min": 272.021,
      "temp_max": 272.021,
      "pressure": 764.35,
      "sea_level": 1036.56,
      "grnd_level": 764.35,
      "humidity": 87,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.14,
      "deg": 359.5
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-10 09:00:00"
  }, {
    "dt": 1512907200,
    "main": {
      "temp": 269.974,
      "temp_min": 269.974,
      "temp_max": 269.974,
      "pressure": 765.13,
      "sea_level": 1038.68,
      "grnd_level": 765.13,
      "humidity": 91,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.06,
      "deg": 357.5
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-10 12:00:00"
  }, {
    "dt": 1512918000,
    "main": {
      "temp": 274.3,
      "temp_min": 274.3,
      "temp_max": 274.3,
      "pressure": 766.45,
      "sea_level": 1040.19,
      "grnd_level": 766.45,
      "humidity": 72,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "02d"
    }],
    "clouds": {
      "all": 8
    },
    "wind": {
      "speed": 1.31,
      "deg": 356
    },
    "rain": {},
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-10 15:00:00"
  }, {
    "dt": 1512928800,
    "main": {
      "temp": 283.213,
      "temp_min": 283.213,
      "temp_max": 283.213,
      "pressure": 766.31,
      "sea_level": 1035.55,
      "grnd_level": 766.31,
      "humidity": 37,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.32,
      "deg": 359.001
    },
    "rain": {},
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-10 18:00:00"
  }, {
    "dt": 1512939600,
    "main": {
      "temp": 286.15,
      "temp_min": 286.15,
      "temp_max": 286.15,
      "pressure": 764.65,
      "sea_level": 1031.43,
      "grnd_level": 764.65,
      "humidity": 32,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.18,
      "deg": 0.000457764
    },
    "rain": {},
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-10 21:00:00"
  }, {
    "dt": 1512950400,
    "main": {
      "temp": 281.575,
      "temp_min": 281.575,
      "temp_max": 281.575,
      "pressure": 765.02,
      "sea_level": 1033.14,
      "grnd_level": 765.02,
      "humidity": 34,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.12,
      "deg": 347
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-11 00:00:00"
  }, {
    "dt": 1512961200,
    "main": {
      "temp": 273.151,
      "temp_min": 273.151,
      "temp_max": 273.151,
      "pressure": 766.48,
      "sea_level": 1037.42,
      "grnd_level": 766.48,
      "humidity": 46,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 0.96,
      "deg": 353.5
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-11 03:00:00"
  }, {
    "dt": 1512972000,
    "main": {
      "temp": 270.195,
      "temp_min": 270.195,
      "temp_max": 270.195,
      "pressure": 766.06,
      "sea_level": 1038.73,
      "grnd_level": 766.06,
      "humidity": 52,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.17,
      "deg": 356.001
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-11 06:00:00"
  }, {
    "dt": 1512982800,
    "main": {
      "temp": 268.356,
      "temp_min": 268.356,
      "temp_max": 268.356,
      "pressure": 765.05,
      "sea_level": 1038.9,
      "grnd_level": 765.05,
      "humidity": 66,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.05,
      "deg": 355.001
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-11 09:00:00"
  }, {
    "dt": 1512993600,
    "main": {
      "temp": 267.209,
      "temp_min": 267.209,
      "temp_max": 267.209,
      "pressure": 764.61,
      "sea_level": 1039.61,
      "grnd_level": 764.61,
      "humidity": 66,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.15,
      "deg": 354.003
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-11 12:00:00"
  }, {
    "dt": 1513004400,
    "main": {
      "temp": 276.151,
      "temp_min": 276.151,
      "temp_max": 276.151,
      "pressure": 765.81,
      "sea_level": 1040.01,
      "grnd_level": 765.81,
      "humidity": 37,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.43,
      "deg": 359.501
    },
    "rain": {},
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-11 15:00:00"
  }, {
    "dt": 1513015200,
    "main": {
      "temp": 286.743,
      "temp_min": 286.743,
      "temp_max": 286.743,
      "pressure": 765.41,
      "sea_level": 1034.66,
      "grnd_level": 765.41,
      "humidity": 27,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.01,
      "deg": 1.50366
    },
    "rain": {},
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-11 18:00:00"
  }, {
    "dt": 1513026000,
    "main": {
      "temp": 289.011,
      "temp_min": 289.011,
      "temp_max": 289.011,
      "pressure": 763.71,
      "sea_level": 1030.39,
      "grnd_level": 763.71,
      "humidity": 22,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.06,
      "deg": 355.501
    },
    "rain": {},
    "sys": {
      "pod": "d"
    },
    "dt_txt": "2017-12-11 21:00:00"
  }, {
    "dt": 1513036800,
    "main": {
      "temp": 282.558,
      "temp_min": 282.558,
      "temp_max": 282.558,
      "pressure": 764.15,
      "sea_level": 1032.17,
      "grnd_level": 764.15,
      "humidity": 30,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 0.87,
      "deg": 348.501
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-12 00:00:00"
  }, {
    "dt": 1513047600,
    "main": {
      "temp": 272.309,
      "temp_min": 272.309,
      "temp_max": 272.309,
      "pressure": 765.77,
      "sea_level": 1036.3,
      "grnd_level": 765.77,
      "humidity": 53,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.01,
      "deg": 348.001
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-12 03:00:00"
  }, {
    "dt": 1513058400,
    "main": {
      "temp": 268.631,
      "temp_min": 268.631,
      "temp_max": 268.631,
      "pressure": 765.93,
      "sea_level": 1037.68,
      "grnd_level": 765.93,
      "humidity": 65,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 1.06,
      "deg": 352
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-12 06:00:00"
  }, {
    "dt": 1513069200,
    "main": {
      "temp": 266.456,
      "temp_min": 266.456,
      "temp_max": 266.456,
      "pressure": 765.26,
      "sea_level": 1037.74,
      "grnd_level": 765.26,
      "humidity": 69,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 0.96,
      "deg": 355.001
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-12 09:00:00"
  }, {
    "dt": 1513080000,
    "main": {
      "temp": 265.114,
      "temp_min": 265.114,
      "temp_max": 265.114,
      "pressure": 764.99,
      "sea_level": 1038.29,
      "grnd_level": 764.99,
      "humidity": 73,
      "temp_kf": 0
    },
    "weather": [{
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01n"
    }],
    "clouds": {
      "all": 0
    },
    "wind": {
      "speed": 0.98,
      "deg": 358.005
    },
    "rain": {},
    "sys": {
      "pod": "n"
    },
    "dt_txt": "2017-12-12 12:00:00"
  }],
  "city": {
    "id": 3526683,
    "name": "Mexico City",
    "coord": {
      "lat": 19.4326,
      "lon": -99.1334
    },
    "country": "MX"
  }
}


temp_at_15=[];
vals=Object.values(data);
vals.forEach(function(el) {
if(Array.isArray(el)) 
el.forEach(function(o) {
if(o.dt_txt.includes('15:00:00'))
 temp_at_15.push(o.main.temp);
});
});

console.log(temp_at_15);


-1

使用 JSON.parse 可能会更容易获取值:

j = '[ { "foo": 1, "bar": 2 }, { "foo": 3, "bar": 4 }, { "foo": 5, "bar": 6 } ]'

a = [], b = JSON.parse(j, function(key, v) { if (v.bar === 2) a.push(v.foo); return v; })

console.log( a ); console.log( b )


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接