如何在JavaScript中从嵌套的json创建数组?

时间:2018-03-27 15:35:33

标签: javascript arrays json

我有一组对象 - http://api.openweathermap.org/data/2.5/forecast?q=london&appid=7ce3e1102e1902e0f878c2a640e95aed

如何在JavaScript中创建三个新数组(单独的变量名称),其中包含" dt"," temp_min"和" temp_max"只能采用以下格式:

dtArray = [firstvalue, secondvalue, thirdvalue, ...]
tempMinArray = [firstvalue, secondvalue, thirdvalue, ...]
tempMaxArray = [firstvalue, secondvalue, thirdvalue, ...]

我循环吗?还是用地图?每?怎么样?

我的不良尝试:

var tempMinArray = new Array;
    for (i = 0; i < results.data.length; i++ ) {
tempMinArray = results.data.list[i].main.temp_min;

}

3 个答案:

答案 0 :(得分:2)

您可以使用Array#map

$.get('https://api.openweathermap.org/data/2.5/forecast?q=london&appid=7ce3e1102e1902e0f878c2a640e95aed')
  .then(function(resp) {
    var dts = resp.list.map(item => item.dt);
    var tempMins = resp.list.map(item => item.main.temp_min);
    var tempMaxs = resp.list.map(item => item.main.temp_max);
    
    console.log('dt:', dts);
    console.log('temp_min:', tempMins);
    console.log('temp_max:', tempMaxs);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:1)

实现方式:

  • 使用单数组filter()方法,并使用ES6语法为所有三个不同的数组迭代数组的对象。

    <强>样本

&#13;
&#13;
var jsonObj = {
	"list": [{
		"dt": 1522227600,
		"main": {
			"temp": 278.73,
			"temp_min": 278.73,
			"temp_max": 278.941,
			"pressure": 1006.02,
			"sea_level": 1013.69,
			"grnd_level": 1006.02,
			"humidity": 100,
			"temp_kf": -0.21
		}
	}, {
		"dt": 1522238400,
		"main": {
			"temp": 279.1,
			"temp_min": 279.1,
			"temp_max": 279.234,
			"pressure": 1004.6,
			"sea_level": 1012.13,
			"grnd_level": 1004.6,
			"humidity": 100,
			"temp_kf": -0.14
		}
	}, {
		"dt": 1522249200,
		"main": {
			"temp": 278.83,
			"temp_min": 278.83,
			"temp_max": 278.898,
			"pressure": 1005.72,
			"sea_level": 1013.37,
			"grnd_level": 1005.72,
			"humidity": 99,
			"temp_kf": -0.07
		}
	}]
};

let dtArray = [];
let tempMinArray = [];
let tempMaxArray = [];

jsonObj.list.filter(obj => {
   (obj.dt) ? dtArray.push(obj.dt) : '';
   (obj.main.temp_min) ? tempMinArray.push(obj.main.temp_min) : '';
   (obj.main.temp_max) ? tempMaxArray.push(obj.main.temp_max) : '';
});

console.log(dtArray);
console.log(tempMinArray);
console.log(tempMaxArray);
&#13;
&#13;
&#13;

  • 根据map()的建议,为我们要创建的每个数组使用单独数组31piy方法。

    <强>样本

&#13;
&#13;
var jsonObj = {
	"list": [{
		"dt": 1522227600,
		"main": {
			"temp": 278.73,
			"temp_min": 278.73,
			"temp_max": 278.941,
			"pressure": 1006.02,
			"sea_level": 1013.69,
			"grnd_level": 1006.02,
			"humidity": 100,
			"temp_kf": -0.21
		}
	}, {
		"dt": 1522238400,
		"main": {
			"temp": 279.1,
			"temp_min": 279.1,
			"temp_max": 279.234,
			"pressure": 1004.6,
			"sea_level": 1012.13,
			"grnd_level": 1004.6,
			"humidity": 100,
			"temp_kf": -0.14
		}
	}, {
		"dt": 1522249200,
		"main": {
			"temp": 278.83,
			"temp_min": 278.83,
			"temp_max": 278.898,
			"pressure": 1005.72,
			"sea_level": 1013.37,
			"grnd_level": 1005.72,
			"humidity": 99,
			"temp_kf": -0.07
		}
	}]
};

var dtArray = jsonObj.list.map(obj => obj.dt);
var tempMinArray = jsonObj.list.map(obj => obj.main.temp_min);
var tempMaxArray = jsonObj.list.map(obj => obj.main.temp_max);

console.log(dtArray);
console.log(tempMinArray);
console.log(tempMaxArray);
&#13;
&#13;
&#13;

  • 使用JavaScript for ... in循环来迭代对象数组。

    <强>样本

&#13;
&#13;
var jsonObj = {
	"list": [{
		"dt": 1522227600,
		"main": {
			"temp": 278.73,
			"temp_min": 278.73,
			"temp_max": 278.941,
			"pressure": 1006.02,
			"sea_level": 1013.69,
			"grnd_level": 1006.02,
			"humidity": 100,
			"temp_kf": -0.21
		}
	}, {
		"dt": 1522238400,
		"main": {
			"temp": 279.1,
			"temp_min": 279.1,
			"temp_max": 279.234,
			"pressure": 1004.6,
			"sea_level": 1012.13,
			"grnd_level": 1004.6,
			"humidity": 100,
			"temp_kf": -0.14
		}
	}, {
		"dt": 1522249200,
		"main": {
			"temp": 278.83,
			"temp_min": 278.83,
			"temp_max": 278.898,
			"pressure": 1005.72,
			"sea_level": 1013.37,
			"grnd_level": 1005.72,
			"humidity": 99,
			"temp_kf": -0.07
		}
	}]
};

var dtArray = [];
var tempMinArray = [];
var tempMaxArray = [];

for (var i in jsonObj.list) {
  dtArray.push(jsonObj.list[i].dt);
  tempMinArray.push(jsonObj.list[i].main.temp_min);
  tempMaxArray.push(jsonObj.list[i].main.temp_max);
}

console.log(dtArray);
console.log(tempMinArray);
console.log(tempMaxArray);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

&#13;
&#13;
$.get("https://api.openweathermap.org/data/2.5/forecast?q=london&appid=7ce3e1102e1902e0f878c2a640e95aed", function(data, r){
  var dArray = data.list.map(a=>a.dt)
  var tempMinArray = data.list.map(a=>a.main.temp_min)
  var tempMaxArray = data.list.map(a=>a.main.temp_max)
  console.log(dArray, tempMinArray, tempMaxArray)
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;