简体   繁体   中英

Getting content of an object in node.js / javascript

I have an object 'res' and it holds a field:

res.headers=new object();

im using this field as a map which holds key and value meaning:

res.headers['key']='value';

is there any way to get the content of this map by iterating it without knowing the key?

thank you!

for(var key in res.headers) {
    if(res.headers.hasOwnProperty(key)) {
        console.log(key + " -> " + res.headers[key]);
    }
}

or with Object.keys() :

for(var key in Object.keys(res.headers)) {
    console.log(key + " -> " + res.headers[key]);
}

Easiest thing to do is use a javascript library, like underscore for example, then use someting like:

arr = _.values(res.headers)
arr[0] // value of first element

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM