string 从数组中删除空字符串同时保持无循环记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19888689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:09:54  来源:igfitidea点击:

Remove empty strings from array while keeping record Without Loop?

javascriptarraysindexingstringremoveall

提问by Universal Grasp

This question was asked here: Remove empty strings from array while keeping record of indexes with non empty strings

这个问题在这里问: 从数组中删除空字符串,同时保留非空字符串的索引记录

If you'd notice the given as @Baz layed it out;

如果您注意到@Baz 列出的内容;

"I", "am", "", "still", "here", "", "man"

"and from this I wish to produce the following two arrays:"

“由此我希望生成以下两个数组:”

"I", "am", "still", "here", "man"

All the Answersto this question referred to a form of looping.

这个问题的所有答案都提到了一种循环形式。

My question: Is there a possibility to removeall indexes with emptystringwithout any looping?... is there any other alternative apart from iterating the array?

我的问题:是否有可能在没有任何循环的情况下删除所有indexes emptystring...除了迭代数组之外还有其他选择吗?

May be some regexor some jQuerythat we are not aware of?

可能是我们不知道的一些regex或一些jQuery

All answers or suggestions are highly appreciated.

非常感谢所有的答案或建议。

回答by Isaac

var arr = ["I", "am", "", "still", "here", "", "man"]
// arr = ["I", "am", "", "still", "here", "", "man"]
arr = arr.filter(Boolean)
// arr = ["I", "am", "still", "here", "man"]

filterdocumentation

filter文件



// arr = ["I", "am", "", "still", "here", "", "man"]
arr = arr.filter(v=>v!='');
// arr = ["I", "am", "still", "here", "man"]

Arrow functions documentation

箭头函数文档

回答by haithamahmed

var newArray = oldArray.filter(function(v){return v!==''});

回答by ErickBest

PLEASE NOTE:The documentation says:

请注意:文档说:

filteris a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of filter in ECMA-262 implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming that fn.call evaluates to the original value of Function.prototype.call, and that Array.prototype.push has its original value.

filter是 ECMA-262 标准的 JavaScript 扩展;因此, 它可能不会出现在标准的其他实现中。您可以通过在脚本的开头插入以下代码来解决此问题,允许在本机不支持它的 ECMA-262 实现中使用过滤器。该算法正是 ECMA-262,第 5 版中指定的算法,假设 fn.call 计算为 Function.prototype.call 的原始值,并且 Array.prototype.push 具有其原始值。

So, to avoid some heartache, you may have to add this code to your script At the beginning.

所以,为了避免一些心痛,你可能不得不将这段代码添加到你的脚本开头

if (!Array.prototype.filter) {
  Array.prototype.filter = function (fn, context) {
    var i,
        value,
        result = [],
        length;
        if (!this || typeof fn !== 'function' || (fn instanceof RegExp)) {
          throw new TypeError();
        }
        length = this.length;
        for (i = 0; i < length; i++) {
          if (this.hasOwnProperty(i)) {
            value = this[i];
            if (fn.call(context, value, i, this)) {
              result.push(value);
            }
          }
        }
    return result;
  };
}

回答by haind

arr = arr.filter(v => v);

as returned vis implicity converted to truthy

返回时v隐式转换为

回答by sansid1983

If are using jQuery, grepmay be useful:

如果使用 jQuery,grep可能有用:



var arr = [ a, b, c, , e, f, , g, h ];

arr = jQuery.grep(arr, function(n){ return (n); });

arris now [ a, b, c, d, e, f, g];

arr就是现在 [ a, b, c, d, e, f, g];

回答by Atif Hussain

i.e we need to take multiple email addresses separated by comma, spaces or newline as below.

即我们需要使用逗号、空格或换行符分隔的多个电子邮件地址,如下所示。

    var emails = EmailText.replace(","," ").replace("\n"," ").replace(" ","").split(" ");
    for(var i in emails)
        emails[i] = emails[i].replace(/(\r\n|\n|\r)/gm,"");

    emails.filter(Boolean);
    console.log(emails);

回答by Esin ?NER

You can use lodash's method, it works for string, number and boolean type

您可以使用 lodash 的方法,它适用于字符串、数字和布尔类型

_.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]

https://lodash.com/docs/4.17.15#compact

https://lodash.com/docs/4.17.15#compact