Last active
September 25, 2019 06:01
-
-
Save xincici/ace1f58c0362ff3ac4322cf4a849def0 to your computer and use it in GitHub Desktop.
destruct object use ... and get a list of data like { key: key, val: val }
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.prototype[Symbol.iterator] = function() { | |
var self = this; | |
var keys = Object.keys(this); | |
var l = keys.length; | |
var i = -1; | |
return { | |
next: function() { | |
i++; | |
var done = i >= l; | |
var key = keys[i]; | |
return { | |
value: done ? void 0 : { | |
key: key, | |
val: self[key], | |
}, | |
done: done | |
} | |
} | |
} | |
} | |
const obj = { | |
a: 1, | |
b: 'asd', | |
c: function() {} | |
}; | |
[...obj] | |
// you will get | |
[ | |
{key: 'a', val: 1}, | |
{key: 'b', val: 'asd'}, | |
{key: 'c', val: function(){}} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment