javascript - Lodash - 确定对象数组是否包含在二维字符串数组中指定的值

标签 javascript arrays lodash

我有一组必填字段名称,其中任一个/任何一个的存在都将被视为有效。我在一个二维数组中这样表示:

const REQUIRED_FIELDS = [
  ['amount', 'presets'],
  ['payment_types', 'credit_card']
]

我将其与看起来像这样的对象数组进行比较:

const FIELDS = [
  {
    type: 'name',
    label: 'Your Name'
  },
  {
    type: 'credit_card',
    label: 'Credit Card'
  }
]

无效示例

const FIELDS = [
  {
    type: 'name',
    label: 'Your Name'
  },
  {
    type: 'credit_card',
    label: 'Credit Card'
  }
]

这将是无效的,因为我们有第二组 (credit_card) 而不是第一组 (amount, presets)

有效示例

const FIELDS = [
  {
    type: 'name',
    label: 'Your Name'
  },
  {
    type: 'credit_card',
    label: 'Credit Card'
  },
  {
    type: 'amount',
    label: 'Amount'
  }
]

我们这里各有一个。

期望的结果

  • bool 数组,例如:

    [[真,假],[真,假]]

  • 或者缺少必填字段的数组,例如:

    [['数量', '预设'], []]

我想我可以用 Lodash 的 some 来完成这件事和 includes ,但我脑子里放了一个大屁。感谢您的帮助。

编辑:

解决方案

这就是最终对我有用的东西。我相信这可以进一步优化。

function validity(fields) {
  const validityMap = REQUIRED_FIELDS.map(arr =>
    fields.map(obj => {
      return obj.type, arr.indexOf(obj.type) > -1
    })
  )
  return validityMap.map(y => y.indexOf(true) > -1)
}

感谢 Greek 先生带我到那里。

最佳答案

你为什么不简单地过滤需要的字段(引用你的第二个输出类型)

REQUIRED_FIELDS.filter(function(el){
    return !FIELDS.some(function(o){
        return el.indexOf(o.type)>-1;
    })
})

const REQUIRED_FIELDS = [
  ['amount', 'presets'],
  ['payment_types', 'credit_card']
];
const FIELDS_1 = [
  {
    type: 'credit_card',
    label: 'Credit Card'
  }
];
const FIELDS_2 = [
  {
    type: 'credit_card',
    label: 'Credit Card'
  },
  {
    type: 'amount',
    label: 'Amount'
  }
];
const FIELDS_3 = [
  {
    type: 'presets',
    label: 'Presets'
  }
];

function getValidity(fields) {
    return REQUIRED_FIELDS.filter(function(el) {
      return !fields.some(function(o) {
        return el.indexOf(o.type)>-1;
      });
    });
}

console.log(getValidity(FIELDS_1));
console.log(getValidity(FIELDS_2));
console.log(getValidity(FIELDS_3));

如果你想要一个空数组来存放所需的字段,那么只需将 .filter 更改为 .map 并按预期更改返回值在 map 而不是过滤器中

const REQUIRED_FIELDS = [
  ['amount', 'presets'],
  ['payment_types', 'credit_card']
];
const FIELDS_1 = [
  {
    type: 'credit_card',
    label: 'Credit Card'
  }
];
const FIELDS_2 = [
  {
    type: 'credit_card',
    label: 'Credit Card'
  },
  {
    type: 'amount',
    label: 'Amount'
  }
];
const FIELDS_3 = [
  {
    type: 'presets',
    label: 'Presets'
  }
];

function getValidity(fields) {
    return REQUIRED_FIELDS.map(function(el) {
      return fields.some(function(o) {
        return el.indexOf(o.type)>-1;
      }) ? [] : el;
    });
}

console.log(getValidity(FIELDS_1));
console.log(getValidity(FIELDS_2));
console.log(getValidity(FIELDS_3));

关于javascript - Lodash - 确定对象数组是否包含在二维字符串数组中指定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46082663/

相关文章:

lodash - 错误: Cannot read property 'cloneDeep' of undefined Angular 5

javascript - 如何使用 lodash/underscore 按多个嵌套字段排序?

javascript - Lodash:如何创建具有新对象结构的数组

javascript - 如何使用javascript计算总和

JavaScript 随机生成 0 或 1 个整数

javascript - 如何摆脱具有早期返回的多个嵌套 switchMap

python - 带有python随机数的大数组

javascript - 按对象数组进行分组和展平以匹配所需的格式

javascript - 我的关联数组不存储对象

javascript - 如何使用 SQL 在 Node JS 中使用特定列值