简体   繁体   English

如何从使用javascript传入对象的数组中获取每个字段数据

[英]How to get each field data from arrays passed in object using javascript

{
  "coord": {
    "lon": 73.86,
    "lat": 18.52
  },
  "sys": {
    "message": 0.0083,
    "country": "IN",
    "sunrise": 1436142797,
    "sunset": 1436190324
  },
  "weather": [
    {
      "id": 500,
      "main": "Rain",
      "description": "light rain",
      "icon": "10d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 298.644,
    "temp_min": 298.644,
    "temp_max": 298.644,
    "pressure": 948.79,
    "sea_level": 1018.56,
    "grnd_level": 948.79,
    "humidity": 78
  },
  "wind": {
    "speed": 6.46,
    "deg": 253.501
  },
  "clouds": {
    "all": 68
  },
  "rain": {
    "3h": 0.225
  },
  "dt": 1436180142,
  "id": 1259229,
  "name": "Pune",
  "cod": 200
}

This is the data we get after requesting current weather api for information of given city .这是我们在请求当前天气 api 以获取给定城市的信息后获得的数据。 My question is to make a loop which get all information by looping through data and get all values我的问题是制作一个循环,通过循环数据并获取所有值来获取所有信息

I don't want to write like this to get data data.coord.lon data.weather.id etc etc Hope you understand A loop which go in to object and get each array and extract each data/property from array我不想这样写来获取数据data.coord.lon data.weather.id等希望你理解一个循环,它进入对象并获取每个数组并从数组中提取每个数据/属性

this is the api for reference http://api.openweathermap.org/data/2.5/weather?q=london have a look这是供参考的api http://api.openweathermap.org/data/2.5/weather?q=london看看

If you mean all the properties for the object received, you could use loop with hasOwnProperty check like here :如果您的意思是接收到的对象的所有属性,您可以使用带有 hasOwnProperty 检查的循环, 如下所示

for (var key in data) {
    if (obj.hasOwnProperty(key)) {
        //key
        //obj[key] value
    }
}

You can use it recursively to get internal properties in the same way.您可以递归地使用它以相同的方式获取内部属性。 UPD Here is the fiddle with recursive solution: http://jsfiddle.net/shershen08/xdwqohz1/ : UPD 这是递归解决方案的小提琴: http : //jsfiddle.net/shershen08/xdwqohz1/

function listKeys(data){
    for (var key in data) {
        if (data.hasOwnProperty(key)) {

            if((typeof data[key]).toLowerCase() != 'object'){
            console.log(key, data[key])
            } else {
                console.log(key + ': ');
                listKeys(data[key]);
            }
        } 
    }

}

How to list the properties of a JavaScript object 如何列出 JavaScript 对象的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JavaScript在对象中存储数据数组 - How to store arrays of data in object using JavaScript 使用javascript es6从包含唯一ID和嵌套数组的多个对象数组中获取公共数据 - Get common data from multiple arrays of object that containing a unique id and nested array using javascript es6 如何使用 Z3AB8B307E489AF2A6C91837B 获取从 java class 到本机脚本 jar 文件的变量数据 - How to get variable data passed from java class to native script jar file using javaScript Javascript Arrays 作为 object 字段 - Javascript Arrays as an object field 如何基于传递给函数的值从javascript中的数据库中获取数据 - How to get data from database in javascript based on the value passed to the function 如何使用javascript从网页获取JSON对象数据 - How to get JSON object data from a webpage using javascript 如何使用Javascript从2D json对象获取数据 - How to get Data from 2D json object using Javascript 如何通过使用Java脚本比较两组数组来获取唯一对象 - How to get the unique object by comparing two sets of arrays using Javascript 如何从“每个”迭代器获取对象字段。 错误的“此”上下文 - How to get object field from 'each' iterator. Wrong 'this' context 如何使用Javascript从GeoJSON获取字段 - How to get field from GeoJSON using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM