简体   繁体   English

当我处理它时,如何确保包含具有某些字符的值的Javascript数组保持不变?

[英]How can I make sure Javascript array containing values with certain character remain unaltered, while I work on it?

Suppose I have an Javascript array, 假设我有一个Javascript数组,

var example = [and, there,sharma<br, />, ok, grt]

Now I want to randomly delete some array values - but not those values which have 现在我想随机删除一些数组值-但不是那些具有

<br

in them, in the above example, I want to make sure 在上面的示例中,我要确保

"sharma<br" is not deleted.

I also do not want to delete "/>". 我也不想删除“ />”。

Can anyone help me. 谁能帮我。 I would really appreciate the answer. 我真的很感谢答案。

First of all, that is not a valid array, unless you are missing the string quotes. 首先,这不是有效的数组,除非您缺少字符串引号。 Anyway, what you are searching for is Array.Filter . 无论如何,您要搜索的是Array.Filter In your case : 在您的情况下:

var filtered = example.filter(v => v.indexOf("<br") != -1 || v.indexOf("/>") != -1)

If I have understood the problem correctly, then you want to keep those entries in the array which have substrings "<br" and "/>" 如果我已正确理解问题,那么您希望将那些具有子字符串"<br"和“ />”的条目保留在数组中

If thats the case, you can try using javascript string method includes() to see if a string contains a particular substring. 如果是这样,您可以尝试使用JavaScript字符串方法includes()来查看字符串是否包含特定的子字符串。

JavaScript array must be created as below JavaScript数组必须如下创建

var example = ["and"," there"",sharma<br","/>","ok"," grt"];

Using splice method , to delete the array values specifying the index positions. 使用splice方法,删除指定索引位置的数组值。

array splice() method changes the content of an array, adding new elements while removing old elements. array splice()方法更改数组的内容,在删除旧元素的同时添加新元素。

Syntax Its syntax is as follows − 语法语法如下-

array.splice(index, howMany, [element1][, ..., elementN]);

Parameter Details 参数明细

index − Index at which to start changing the array. index-开始更改数组的索引。

howMany − An integer indicating the number of old array elements to remove. howMany-一个整数,指示要删除的旧数组元素的数量。 If howMany is 0, no elements are removed. 如果howMany为0,则不会删除任何元素。

element1, ..., elementN − The elements to add to the array. element1,...,elementN-要添加到数组中的元素。 If you don't specify any elements, splice simply removes the elements from the array. 如果您未指定任何元素,则拼接只会从数组中删除这些元素。

Return Value Returns the extracted array based on the passed parameters. 返回值根据传递的参数返回提取的数组。

var removed = arr.splice(2, 2);

This would remove your suggested output to be my assumption . 这将删除您建议的输出作为我的假设。

暂无
暂无

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

相关问题 Javascript:如何检查数组是否包含某些值 - Javascript: How can i check if a array contains certain values 如何确保将计时器设置为每天4点半,所以我不必在Javascript中提及某个日期? - How can I make sure the timer is set to half 4 every day, so I don't have to mention a certain date in Javascript? 如何迭代包含单个字符串的 JavaScript 数组? - How Can I Iterate over JavaScript array containing single character strings? 如何从 JavaScript 中的数组中排除包含某些字符的数据? - How can I exclude data containing certain characters from an array in JavaScript? JavaScript:如何确保此函数仅返回完全填充的数组? - JavaScript: How can I make sure that this function only returns the fully populated array? JavaScript:如何使Var“数组”工作? - JavaScript: How Can I Make A Var “Array” Work? JavaScript 如何确保下载的文件成功且未损坏 - JavaScript how can I make sure the downloaded file is successful and not corrupted 如何确保某个集合中的数据先于其他数据到达? - How can I make sure that data from a certain Collection arrives before other data? 如何制作 JavaScript while 循环来存储可被两个值整除的值? - How can I make a JavaScript while-loop for storing values that are divisible by two values? Javascript:如何获取数组中特定顺序值的索引? - Javascript: How do I get the index of a certain order of values in an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM