【问题标题】:Cannot produce NULL Javascript无法生成 NULL Javascript
【发布时间】:2021-08-24 22:11:53
【问题描述】:

第一次发帖,此时感觉有点失落。我试图在我的 json 对象中重现 Null 的结果。目前结果如下:

{ 重叠贸易线:假, time_for_overlap_months:628, minimum_two_years_overlapping:假, 版本:'08.23.21' }

我希望它产生:

{ 重叠贸易线:假, time_for_overlap_months:空, minimum_two_years_overlapping:假, 版本:'08.23.21' }

我尝试执行第二个 if 语句,但无济于事。为了获得我想要达到的结果,我是否需要实施 else if?这是我的工作代码: *请注意,为简化代码,已删除 xml 路径。如果您想深入挖掘这些文件,请告诉我:)

const jsonOutput = {};
let isOverLap = false;
let openDate = null;
let minimum_two_years_overlapping = false;

var stop = false;

for (let i = 1; i <= applicantSegLen; i++) {
  if (stop == true) break;
  const account = xpath.select("string(//TRsegments[1]/TRsegment[" + i + "]/@account)", doc);
  const dateopen = xpath.select("string(//TRsegments[1]/TRsegment[" + i + "]/@dateopen)", doc);
  const balance = xpath.select("string(//TRsegments[1]/TRsegment[" + i + "]/@balance)", doc);

  for (let j = 1; j <= coApplicantSegLen; j++) {
    if (stop == true) break;
    const account2 = xpath.select("string(//TRsegments[1]/TRsegment[" + j + "]/@account)", coDoc);
    const dateopen2 = xpath.select("string(//TRsegments[1]/TRsegment[" + j + "]/@dateopen)", coDoc);
    const balance2 = xpath.select("string(//TRsegments[1]/TRsegment[" + j + "]/@balance)", coDoc);

    if (account === account2 && dateopen === dateopen2 && balance === balance2) {
      openDate = dateopen;
      isOverLap = true;
      stop = true;
    }
    }
  }


//diff is the conversion for opendate to today's date. It converts milliseconds to months
jsonOutput["overlapping_tradelines"] = isOverLap;
const now = Date.now();
openDate = new Date(openDate);

let diff = now - openDate;
diff = diff / (30 * 24 * 60 * 60 * 1000);
const months = Math.floor(diff);

jsonOutput["time_for_overlap_months"] = months;
minimum_two_years_overlapping = diff == 24;
jsonOutput["minimum_two_years_overlapping"] = minimum_two_years_overlapping;

let version = "08.23.21";
jsonOutput["version"] = version;

console.log(jsonOutput);

【问题讨论】:

  • 您希望语句jsonOutput["time_for_overlap_months"] === null;jsonOutput["minimum_two_years_overlapping"] === false; 做什么?请参阅In javascript == vs =?
  • 如果“overlapping_tradelines”等于 false,我希望这些语句以这种方式显示。
  • ["overlapping_tradelines"] === false 始终为 false"overlapping_tradelines" === true 始终是 false“如果“overlapping_tradelines”等于 false”,那么 什么?这些语句什么都不做。你的整个较低的if 语句永远不会被执行。
  • 我明白了,我认为这将是解决方案。我的原始代码不存在整个较低的if 语句。是否有不同的方法来实现我正在寻找的结果?
  • 您只需设置jsonOutput["time_for_overlap_months"] = isOverlap ? months : nulldocs

标签: javascript if-statement xpath null


【解决方案1】:

这是@Charles Bamford 建议的更新回复,添加了三元 if/else:

jsonOutput["overlapping_tradelines"] = isOverLap;
const now = Date.now();
openDate = new Date(openDate);

let diff = now - openDate;
diff = diff / (30 * 24 * 60 * 60 * 1000);
const months = Math.floor(diff);
jsonOutput["time_for_overlap_months"] = isOverLap ? months : null

minimum_two_years_overlapping = diff == 24;
jsonOutput["minimum_two_years_overlapping"] = minimum_two_years_overlapping;

let version = "08.23.21";
jsonOutput["version"] = version;

console.log(jsonOutput);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-19
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2017-10-04
    相关资源
    最近更新 更多