【问题标题】:How to not proceed with adding a value if its an empty/null string?如果它是一个空/空字符串,如何不继续添加一个值?
【发布时间】:2016-12-03 18:12:59
【问题描述】:

我的代码在某处读取数据,然后将它们作为值添加到对象的属性中。我做了一个 for 循环来遍历列表中的项目,这样它们每个都可以属于自己可爱的小对象,但这里有一个问题:有时属性没有价值,我不知道我能做些什么来拦截在它完成向财产声明之前。也许用单词或其他东西替换空字符串。

这是一个示例代码,假设第 3 次迭代的标题将是一个空的 "" 字符串。我该如何拦截?

var counter = 0;
for (var i = 0; i < nyData.results.length; i++) {
  if (_.indexOf(uniqueItems, nyData.results[i].id)) {
    continue;
  }
  if (!_.indexOf(uniqueItems, nyData.results[i].id)) {
    var index = i;
    counter++;
    let putParams = {
        TableName: "Articles",
        Item: {
          "title": nyData.results[i].title,
          "date": nyData.results[i].published_date,
          "abstract": nyData.results[i].abstract,
          "source": nyData.results[i].source,
          "views": nyData.results[i].views,
          "author": nyData.results[i].byline,
          "section": nyData.results[i].section,
          "category": nyData.results[i].des_facet,
          "organizations": nyData.results[i].org_facet,
          "people_involved": nyData.results[i].per_facet,
          "country_subject": nyData.results[i].geo_facet,
          "id": nyData.results[i].id,
        }
    }
    db.put(putParams, function(err) {});
  }
}
console.log(`Total of ${counter} new articles were inserted into database.`);
callback(null);

【问题讨论】:

  • console.log(`Total of ${counter} new articles were inserted into database.`); 这是什么语言?
  • 请停止使用虚假标签毒害您的问题标题。我们有一个实际的标签系统。
  • @LightnessRacesinOrbit 看起来像 JavaScript,正如 OP 所说....?
  • @qxz:对我来说看起来不像 JavaScript。 ${counter} 是 Perl(或 PHP?),我不知道这些反引号在什么语言中有效。
  • @LightnessRacesinOrbit 它被称为template literal(ES6 特性)。

标签: javascript amazon-web-services object amazon amazon-dynamodb


【解决方案1】:
if( myVar) {
}

只有当变量不是时才会为真:-



未定义
0

....还有更多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2011-04-07
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多