简体   繁体   中英

How to separate key and value pair from an object in typescript

I would like to seperate the key and value from an object in typescript for that i have used the following code which returns only values but keys are not displayed.

Object.keys(data).forEach(key=> {
    console.log('keys', data[key]);     
});

But when i use the below function in javascript it gives me the key and value correctly can anyone tell me how to do the same in typescript to get the key and values from an object.

angular.forEach(data, function (value, column) {
    columns.push(column);
    values.push(value);
  });

What you get with data[key] is the value:

Object.keys(data).forEach(key => {
    console.log('key', key);     
    console.log('value', data[key]);     
});

If you want to loop over an object, you can simply use for...in too:

for (var key in data) {
    if (data.hasOwnProperty(key)) {
        console.log('key', key);
        console.log('value', data[key]);
    }
}

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