确定 Javascript 对象中的不完整部分

问题描述 投票:0回答:2

我有一个具有多个属性的 JavaScript 对象:

{
  banking: {
    account_number: null,
    account_type: null,
    bank_name: null,
    debit_day: null
  },
  fitment: {
    date: null,
    terms: null
  },
  personal_info: {
    email: null,
    IDNumber: null,
    mobile: null,
    name: null,
    residential_address: null,
    surname: null,
    title: null,
    work_address: null,
    work_tel: null
  },
  vehicle: {
    brand: null,
    colour: null,
    model: null,
    registration: null,
    vin: null,
    year: null
  }
}

所有顶级属性和嵌套属性的默认值为

null
.

我正在尝试找出一种将属性分为三组的方法,即:

部分完整

empty”是一个部分中的所有值都设置为

null
.

的情况

partial”是部分中的某些值已设置(并非所有

null

complete”是一个部分中没有任何值设置为

null
。都有价值。

我的第一次尝试是将 Underscore 库与

_.some()
一起使用,但是我似乎无法全神贯注于满足所有组场景。

一些帮助和指导将不胜感激。

谢谢!

javascript underscore.js
2个回答
1
投票

您可以先检查

every
,然后再检查
some
,或者默认为
empty

const main = () => {
  const sectionStatus = Object.fromEntries(
    Object.entries(sections).map(([section, data]) => [
      section,
      Object.entries(data).every(([id, value]) => value !== null)
        ? 'complete'
        : Object.entries(data).some(([id, value]) => value !== null)
          ? 'partial'
          : 'empty'
    ]));
  
  console.log(sectionStatus);
};

const sections = {
  banking: {
    account_number: '555',
    account_type: null,
    bank_name: null,
    debit_day: null
  },
  fitment: {
    date: new Date(),
    terms: 'foobar'
  },
  personal_info: {
    email: null,
    IDNumber: null,
    mobile: null,
    name: null,
    residential_address: null,
    surname: null,
    title: null,
    work_address: null,
    work_tel: null
  },
  vehicle: {
    brand: null,
    colour: null,
    model: null,
    registration: null,
    vin: null,
    year: null
  }
};

main();
.as-console-wrapper { top: 0; max-height: 100% !important; }

输出

{
  "banking": "partial",
  "fitment": "complete",
  "personal_info": "empty",
  "vehicle": "empty"
}

如果你想优化这个,你可以用

1
(不是
null
)或
0
null
)标记每个字段并减少值。

const main = () => {
  const sectionStatus = Object.fromEntries(
    Object.entries(sections).map(([section, data]) => { 
      const flags = Object.values(data).map((v) => v !== null ? 1 : 0);
      const state = flags.reduce((a, b) => a & b, 1) === 1 // or (a * b)
        ? 'complete'
        : flags.reduce((a, b) => a | b, 0) === 1 // or (a + b) > 0
          ? 'partial'
          : 'empty';
      return [section, state];
    }, []));
  
  console.log(sectionStatus);
};

const sections = {
  banking: {
    account_number: '555',
    account_type: null,
    bank_name: null,
    debit_day: null
  },
  fitment: {
    date: new Date(),
    terms: 'foobar'
  },
  personal_info: {
    email: null,
    IDNumber: null,
    mobile: null,
    name: null,
    residential_address: null,
    surname: null,
    title: null,
    work_address: null,
    work_tel: null
  },
  vehicle: {
    brand: null,
    colour: null,
    model: null,
    registration: null,
    vin: null,
    year: null
  }
};

main();
.as-console-wrapper { top: 0; max-height: 100% !important; }


0
投票

您还可以使用

_.groupBy
进一步分解这个问题。首先,一个函数只是为了确定单个部分的类别:

function category(section) {
    return _.every(section, _.isNull) ? 'empty' :
           _.some(section, _.isNull) ? 'partial' : 'complete';
}

然后,结合

_.groupBy
为每个类别创建一个原始对象数组:

_.groupBy(sections, category)

把它们放在一起:

var sections = {
  banking: {
    account_number: null,
    account_type: null,
    bank_name: null,
    debit_day: null
  },
  fitment: {
    date: null,
    terms: null
  },
  personal_info: {
    email: null,
    IDNumber: null,
    mobile: null,
    name: 'Jane Doe',
    residential_address: null,
    surname: null,
    title: null,
    work_address: null,
    work_tel: null
  },
  vehicle: {
    brand: null,
    colour: null,
    model: null,
    registration: null,
    vin: null,
    year: null
  }
};

function category(section) {
    return _.every(section, _.isNull) ? 'empty' :
           _.some(section, _.isNull) ? 'partial' : 'complete';
}

console.log(_.groupBy(sections, category));
<script src="https://cdn.jsdelivr.net/npm/[email protected]/underscore-umd-min.js"></script>

© www.soinside.com 2019 - 2024. All rights reserved.