解析 Javascript 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13705203/
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
Parsing Javascript Array
提问by wowzuzz
I have an array as a attribute on a link.
我有一个数组作为链接上的属性。
Here is the array
这是数组
images="["one.jpg","two.jpg"]"
How would I parse through this array and have it read back to me one.jpg,two.jpg?
我将如何解析这个数组并将它读回给我 one.jpg,two.jpg?
This is what I am doing now and it is giving me an error back. I don't believe json parsing is whats needed here.
这就是我现在正在做的事情,它给了我一个错误。我不相信这里需要 json 解析。
var imgs = $("#"+number).attr("images");
var imgList = jQuery.parseJSON(imgs);
EDIT: ACTUAL CODE
编辑:实际代码
var number = $(this).attr("data-id");
var url = $("#"+number).attr("url");
$(".portfolio-url").html("<h3 class='pacifico'>url</h3><p><a href='http://"+url+"' target='_blank'>"+url+"</a></p>");
var cli = $("#"+number).attr("client");
$(".portfolio-client").html("<h3 class='pacifico'>client</h3><p>"+cli+"</p>");
var pgs = $("#"+number).attr("pages");
pgs = pgs.replace(/\[/g,"");
pgs = pgs.replace(/\]/g,"");
pgs = pgs.replace(/\"/g,"");
var pages = new Array();
pages = pgs.split(",");
var img = $("#"+number).attr("images");
img = img.replace(/\{/g,"");
img = img.replace(/\}/g,"");
img = img.replace(/\"/g,"");
var images = new Array();
images = img.split(",");
var portSkills = "<h3 class='pacifico'>skills</h2>";
portSkills += "<p>";
for (i=0;i<pages.length;i++) {
if (pages[i] != "Clients") {
var finalPage = "";
for (j=0;j<pages[i].length;j++)
{
var ch = pages[i].charAt(j);
if (ch == ch.toUpperCase()) {
finalPage += " ";
}
finalPage += pages[i].charAt(j);
}
portSkills += finalPage+"<br />";
}
}
portSkills += "</p>";
$(".portfolio-skills").html(portSkills);
var imgs = $("#"+number).attr("images");
var imgList = jQuery.parseJSON(imgs);
Basically, its looping through parameters
基本上,它通过参数循环
采纳答案by Sampson
I'd encourage you to modify your attribute-value format to something along these lines:
我鼓励您将属性值格式修改为以下几行:
<div id="one" data-images="file1.jpg,file2.jpg">Foo, Bar</div>???????????????????????????????????????????????????????
Note here I'm using a valid data-
attribute, and the value of this attribute is just a list of comma-separated filenames. No need to place [
or ]
in this value in order to get an array.
注意这里我使用了一个有效的data-
属性,这个属性的值只是一个逗号分隔的文件名列表。无需将[
或放置]
在此值中即可获得数组。
Now, to get your array:
现在,要获取您的数组:
var images = $("#one").data("images").split(",");
Which results in the following array:
这导致以下数组:
["file1.jpg", "file2.jpg"]
回答by xdazz
Don't put that kind of string in the attribute, you could just put a comma separated string instead. (And you could use data attribute.)
不要在属性中放置那种字符串,您可以只放置一个逗号分隔的字符串。(你可以使用 data 属性。)
For example:
例如:
<a id="foo" data-images="one.jpg,two.jpg">foo</a>
then you could get it by:
那么你可以通过以下方式获得它:
var imgList = $('#foo').data('images').split(',');
回答by knownasilya
Give the join()
methoda try:
试试这个join()
方法:
images.join();
=> "one.jpg,two.jpg"
images.join(", ");
=> "one.jpg, two.jpg"
Edit:To declare your Array
:
编辑:声明您的Array
:
var images = ["img1", "img2", "img3"];
or
或者
var images = new Array("img1", "img2", "img3");
Then you can use the join()
method, and if that still doesn't work, try the following:
然后您可以使用该join()
方法,如果仍然不起作用,请尝试以下操作:
// should be true, if not then you don't have an Array
var isArray = (images instanceof Array);
回答by Slevin
for (var i = 0; i < images.length; i++) {
var image = images[i];
}
回答by Henrik Andersson
For starters:
对于初学者:
images = ["one.jpg", "two.jpg"];
is an array, yours is invalid.
images = ["one.jpg", "two.jpg"];
是一个数组,你的无效。
to have it read back to you
让它读给你听
for(image in images)
console.log(images[image]);
or the jQuery way
或 jQuery 方式
$.each(images, function(index){
console.log(images[index]);
});
if its a String that you need to split then but that is of course if the string looks like this
如果它是一个你需要拆分的字符串,但当然,如果字符串看起来像这样
var img = '["one.jpg", "two.jpg"]';
var images = img.replace(/\[|\]|"| /g,'').split(',');
this will give you an array parsed from a string that looks like an array.
这将为您提供一个从看起来像数组的字符串解析的数组。