Skip to content

Instantly share code, notes, and snippets.

@xincici
Last active September 25, 2019 06:01
Show Gist options
  • Save xincici/ace1f58c0362ff3ac4322cf4a849def0 to your computer and use it in GitHub Desktop.
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 }
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