繁体   English   中英

按键和值过滤 Object

[英]Filter Object by key and value

无论如何要过滤 object 有条件地检查键和值? 我想做的是检查键是否等于字符串并检查值是否为空。

我正在尝试检测 Teaching Hospital 是否具有空值,但 Teaching Hospital Tax ID 没有,反之亦然。 如果教学医院名称有值,教学医院税号没有,则返回结果为:{教学医院税号:""}。

Object

{
  'Manufacturer Name': 'Ascendis',
  'HCP First Name': 'John',
  'HCP Middle Name': '',
  'HCP Last Name': '',
  'HCP NPI': 2442,
  'HCP Credentials': '',
  'HCP State License Number': '',
  'HCP State of Licensure': '',
  'HCP Specialty': '',
  'Teaching Hospital Name': 'Trinity',
  'Teaching Hospital Tax ID': '',
  'Address 1': '',
  'Address 2': '',
  'Postal Code': '',
  'Spend Date': '',
  'Spend Currency': '',
  'Spend Amount': '',
  'Form of Payment': '',
  'Nature of Payment': '',
  'Home System Identifier': '',
  'Product Name': '',
  'Travel City': '',
  'Travel State': '',
  'Travel Country': ''
}

代码

Object.entries(item)
          .filter(([key, value]) => (key === "Teaching Hospital Name" && value != "") && (key === "Teaching Hospital Tax ID" && value === ""))

使用|| 而不是&&

 const obj = { 'Manufacturer Name': 'Ascendis', 'HCP First Name': 'John', 'HCP Middle Name': '', 'HCP Last Name': '', 'HCP NPI': 2442, 'HCP Credentials': '', 'HCP State License Number': '', 'HCP State of Licensure': '', 'HCP Specialty': '', 'Teaching Hospital Name': 'Trinity', 'Teaching Hospital Tax ID': '', 'Address 1': '', 'Address 2': '', 'Postal Code': '', 'Spend Date': '', 'Spend Currency': '', 'Spend Amount': '', 'Form of Payment': '', 'Nature of Payment': '', 'Home System Identifier': '', 'Product Name': '', 'Travel City': '', 'Travel State': '', 'Travel Country': '' } Object.entries(obj).forEach(([key, value]) => { if ((key === "Teaching Hospital Name" && value.= "") || (key === "Teaching Hospital Tax ID" && value === "")) { delete obj[key] } }) console.log(obj)

您可以使用Array.prototype.reduce创建一个新的 object 并移除 select 属性:

Object.entries(obj).reduce((acc, [key, value]) => {
  if (
    (key === "Teaching Hospital Name" && value !== "") ||
    (key === "Teaching Hospital Tax ID" && value === "")
  ) {
    // do not add key/value to resulting object
    return { ...acc };
  }

  return { ...acc, [key]: value };
}, {});

这避免了改变原始的 object。

让我们分析一下我们得到了什么和我们想要什么。

  • 我们有一个 model obj
  • 我们想要选择特定的键并对选择的键/值运行特定的检查
  • 支票返回 object 以及那些值为空字符串的键
  • 此外,如果所有值都为空,检查将忽略结果

我们可以把上面写成

1. if(allValues has an empty string) return false // ignore results
2. if(allValues has NOT an empty string) return false // ignore 3. results
3. return object with those keys where value is empty string

在 JS 中看起来像

// your model
const obj = {}
// your group of keys you want to pick from object
const group = [];
// picked [[key,value]] defined by your group
const entries = Object.entries(obj).filter(([key]) => group.includes(key));
// filter out those which are empty
const emptyEntries = entries.filter(([key, value]) => value === ""); 
// 1. step - to check if all values are empty we can check lengths
if(entries.length === emptyEntries.length) return false;
// 2. step - just check if filtered length is 0
if(emptyEntries.length === 0) return false
// 3. step - convert your emptyEntries to object
return Object.fromEntries(emptyEntries);

让我们看看下面的代码是否适合您

 const obj = { 'Manufacturer Name': 'Ascendis', 'HCP First Name': 'John', 'HCP Middle Name': '', 'HCP Last Name': '', 'HCP NPI': 2442, 'HCP Credentials': '', 'HCP State License Number': '', 'HCP State of Licensure': '', 'HCP Specialty': '', 'Teaching Hospital Name': 'Trinity', 'Teaching Hospital Tax ID': '', 'Address 1': '', 'Address 2': '', 'Postal Code': '', 'Spend Date': '', 'Spend Currency': '', 'Spend Amount': '', 'Form of Payment': '', 'Nature of Payment': '', 'Home System Identifier': '', 'Product Name': '', 'Travel City': '', 'Travel State': 'nonempty', 'Travel Country': '' } function check(obj, groupArr) { const group = new Set(groupArr); const vals = Object.entries(obj).filter(([key, value]) => group.has(key) && value === ""); if(vals.length === group.size || vals.length === 0) return false; return Object.fromEntries(vals); } console.log(check(obj, ["Travel City", "Travel State", "Travel Country"])); /* results { "Travel City": "", "Travel Country": "" } */

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM