我正在使用lodash reject为了拒绝对象数组的多个可能的属性。
const operatingCountries = reject(operating_countries, ['country', null]);
或者
const operatingCountries = reject(operating_countries, ['country', ""]);
或者
const operatingCountries = reject(operating_countries, ['country', false]);
所以我需要做类似的事情:
const operatingCountries = reject(operating_countries, ['country', null || 'country', ""]);
所以我想测试 country
属性来检查它是否带有字符串。不是空字符串。我需要它带有一个字符串值,我不需要任何 false
、null
、""
等。 .
那么我有什么选择?
最佳答案
您可以使用Array.filter()
或lodash过滤器,并返回值:
const operating_countries = [{ country: '' }, { country: 'cats' }, { country: null }]
const result = operating_countries.filter(({ country }) => country)
console.log(result)
或者使用lodash:
const operating_countries = [{ country: '' }, { country: 'cats' }, { country: null }]
const result = _.filter(operating_countries, 'country')
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
关于javascript - 如何使用 lodash拒绝来拒绝同一 key 的多个可能属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56663037/