【发布时间】:2020-12-15 05:38:14
【问题描述】:
eslint 不断向我显示prefer-restructuring 错误。但是,我真的不知道数组解构是如何工作的,希望得到一些帮助。
这是返回错误的两行:
word.results.inCategory = word.results.inCategory[0];
// and:
word.results = word.results.filter(
(res) =>
Object.keys(res).includes('partOfSpeech') &&
Object.keys(res).includes('inCategory')
)[0];
再说一次,我在这方面的知识不是很渊博,所以如果能提供任何关于如何解决/简化这个问题的帮助,我们将不胜感激!
编辑:这是一个示例对象供参考:
{
word: 'midrash',
results: [{
definition: '(Judaism) an ancient commentary on part of the Hebrew scriptures that is based on Jewish methods of interpretation and attached to the biblical text',
partOfSpeech: 'noun',
inCategory: ['judaism'],
typeOf: [ 'comment', 'commentary' ]
},
{
definition: 'something',
partOfSpeech: 'something',
}],
syllables: { count: 2, list: [ 'mid', 'rash' ] },
pronunciation: { all: "'mɪdrɑʃ" },
frequency: 1.82
}
【问题讨论】:
-
您应该创建一个最小的可重现示例(最好在 codesandbox 上)
-
您不能使用 destructuring 重新定义对象的属性,这意味着您必须使用两个语句,例如
const { results: { inCategory: [ category ] } } = word; word.results.inCategory = category;
标签: javascript node.js arrays ecmascript-6 eslint