javascript - 如何使用 lodash拒绝来拒绝同一 key 的多个可能属性?

标签 javascript ecmascript-6 lodash

我正在使用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 属性来检查它是否带有字符串。不是空字符串。我需要它带有一个字符串值,我不需要任何 falsenull"" 等。 .

那么我有什么选择?

最佳答案

您可以使用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/

相关文章:

javascript - ES6 模块 : Export single class of static methods OR multiple individual methods

javascript - 获取reactjs中组件的可选 Prop 列表

javascript - React组件属性中es6模板字符串

javascript - 使用 Lodash 遍历 JavaScript 对象中的属性

javascript - 使用 lodash 从数组返回对象属性

javascript - 当我的 iframe 加载时,会出现两个滚动条,如果我将 iframe 的滚动设置为 no,它将不会显示网站的完整内容

javascript - 当元素相互包含时如何在元素上获取 jquery onClick 事件

javascript - 同步 JavaScript 函数

javascript - 在 ng 上访问范围功能 - 单击导航栏

javascript - 使用 Lodash 中其他两个 JSON 数组的所有组合标记数据项