【发布时间】:2022-01-24 09:51:50
【问题描述】:
我有一个 Javascript 对象:
const mapping = {
'Mind Management': {
type: 'average',
linked_topics: ['Digital Detox', 'Small Scaling', 'Simplexity'],
edge: 'Zeroed Out'
},
'Ancient Wisdom': {
type: 'direct',
edge: 'Roots Revival'
}
};
我想遍历这个对象并检查对象的key 或linked_topics(如果存在)是否与字符串值匹配。
const stringToBeMatched = 'Simplexity'
我尝试过的代码:
for (var key in mapping) {
if(key === stringToBeMatched || mapping[key].linked_topics.includes(stringToBeMatched) ) {
console.log(`found ${stringToBeMatched} in ${key}`);
}
}
我收到以下 eslint 错误:
ESLint: for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array.(no-restricted-syntax)
如何解决这个问题?有没有更好的方法可以在不使用 for..in 的情况下实现这一点?
【问题讨论】:
-
你期待什么结果?外部 ptoperty 名称或数组的索引或两者兼而有之?
标签: javascript object eslint