Only arrays have a forEach
method; data.forEach
won't work, since data
isn't an array. You need this
data.forEach((previousKey) => {
to be
Object.keys(data).forEach((previousKey) => {
or, if you want the associated value too
Object.entries(data).forEach(([previousKey, value]) => {
For point 2, I do want to iterate over every property.
I don't think you deliberately want to iterate over inherited properties as well. Do you really have properties on internal prototypes, eg
const obj = Object.create({ foo: 'bar' });
obj.someProp = 'someVal';
, and you'd want to iterate over both foo
and someProp
?
That sounds quite unusual - possible, but very weird. That's why the linter is warning you against using for..in
. If you do need to iterate over inherited properties, either disable the linter rule for this one line (preferable, since that'll make the code easiest), or (much more verbosely) iterate over the own-properties of the instance object with Object.keys
, then do the same for the instance's prototype, and so on, until you reach the end of the prototype chain.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…