Javascript JS - 拆分字符串并循环遍历结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8394163/
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-08-24 05:49:50  来源:igfitidea点击:

JS - Splitting a string and looping through results

javascript

提问by TheCarver

In JS, I'm having trouble working out how to split a string coming from an AJAX call.

在 JS 中,我在弄清楚如何拆分来自 AJAX 调用的字符串时遇到了麻烦。

This is what I have so far:

这是我到目前为止:

xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
feedUpdateResponse = xmlhttp.responseText;
/////...split script.../////
}
}
xmlhttp.open("GET","https://myDomain.com/myScript.aspx",true);
xmlhttp.send();

Where you have /////...split script...///// in my script above, I need to add a little function that splits the string returned from my AJAX call.

在我上面的脚本中有 /////...split script...///// 的地方,我需要添加一个小函数来拆分从我的 AJAX 调用返回的字符串。

The string simply contains names of DIVs, like this:

该字符串仅包含 DIV 的名称,如下所示:

feedUpdateResponse = "div1/div2/div3/div4"

I would like to first split the string by its slashes (/) and run a loop through the different values and do stuff to those elements on my page.

我想首先用斜杠 (/) 分割字符串,然后循环遍历不同的值并对页面上的这些元素执行操作。

To give an idea of what I need to achieve, I have given this example which is a mix of ASP & JS - it's the only way I can possibly describe it (and show that I've had an attempt) :)

为了了解我需要实现的目标,我给出了这个混合了 ASP 和 JS 的示例——这是我可能描述它的唯一方式(并表明我已经尝试过):)

MyArray = Split(feedUpdateResponse,"/")
For Each X In MyArray
documentGetElementById('updateAvailable_'+x).style.visibility="visible";
Next

On my page I have an ASP script that produces jquery carousels, all contained by separate DIVs. The DIVs are named DIV1, DIV2 etc. Inside DIV1, for example, is a text element called updateAvailable_div1which will alert the user "There are new photos available for this feed, please click the refresh button".

在我的页面上,我有一个生成 jquery 轮播的 ASP 脚本,所有这些都包含在单独的 DIV 中。DIV 被命名为 DIV1、DIV2 等。例如,在 DIV1 中,有一个名为的文本元素updateAvailable_div1,它会提醒用户“此提要有可用的新照片,请单击刷新按钮”。

Could somebody please explain to me how I can change my example above to work in JS? Just need to split the string into an array and loop through the split values...

有人可以向我解释我如何更改上面的示例以在 JS 中工作吗?只需要将字符串拆分为一个数组并循环遍历拆分值...

回答by gilly3

Get your array via string.split("/"). Iterate your array using your method of choice. I prefer Array.forEach():

通过string.split("/"). 使用您选择的方法迭代您的数组。我更喜欢Array.forEach()

feedUpdateResponse.split("/").forEach(function (item) {
    document.getElementById(item).style.visibility = "visible";
});

See the compatibility notesfor using .forEach()in older browsers.

请参阅在旧浏览器中使用的兼容性说明.forEach()

回答by nnnnnn

You can use .split()to split a string on a specified character with the results returned as an array. So then it's just a matter of looping through the array:

您可以使用.split()在指定字符上拆分字符串,并将结果作为数组返回。那么这只是遍历数组的问题:

// given your existing variable
// feedUpdateResponse = "div1/div2/div3/div4" as set in the
// code in the question, add this:

var a = feedUpdateResponse.split("/"),
    i;

for (i = 0; i < a.length; i++) {
    document.getElementById("updateAvailable_" + a[i]).style.visibility
                                                                 = "visible";
}

回答by John Hartsock

var feedUpdateResponse = "div1/div2/div3/div4";
var feedUpdateSplit = feedUpdateResponse.split("/");

for (var x = 0; x < feedUpdateSplit.length; x++) {
  document.getElementById("updateAvailable_" + feedUpdateSplit[x]).style.visibility = "visible";
}

回答by Christopher Pelayo

Try this code:

试试这个代码:

var a = feedUpdateResponse.split("/");

for (i in a) {
    document.getElementById("updateAvailable_" + a[i]).style.visibility
                                                                 = "visible";
}