如何用一种更有说服力的方式(最好是使用Lodash)说:
anObject = {
'a': 'apple',
'b': 'banana'
};
largeArray = ['a', 'c', 'd'];
emptyArray = [];
for (var s = 0; s < largeArray.length; s++) {
if (anObject[largeArray[s]]) {
emptyArray.push(anObject[largeArray[s]]);
}
}
console.log(emptyArray)
应该给我:
emptyArray = ['a']
最佳答案
您可以从largeArray
获取intersection(),从anObject
变量获取keys()以完成此操作。
_.intersection(largeArray, _.keys(anObject));
var anObject = {'a': 'apple', 'b': 'banana'};
var largeArray = ['a','c','d'];
var emptyArray = _.intersection(largeArray, _.keys(anObject));
console.log(emptyArray);
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>