How can I make a multidimensional array with structure like this one for each li with class sl-item:
$imagesList = [
[1, 123, "<img src=\"/img-src/123.jpg \" />", "/photo/img-123", "image-alt "],
[2, 452, "<img src=\"/img-src/452.jpg \" />", "/photo/img-452", "image-alt "],
];
First should be just number of array, 1,2,3..., second part is the id of li element, third is the img with img src, fourth is the url from a with class photo-url and fifth is the img alt.
My structure is the following:
<ul class="sl-img">
<li id="123" class="sl-item">
<a class="photo-url" href="/photo/img-123">
<img alt="image-alt" src="/img-src/123.jpg" >
</a>
</li>
<li id="452" class="sl-item">
<a class="photo-url" href="/photo/img-452">
<img alt="image-alt" src="/img-src/452.jpg" >
</a>
</li>
.....
</ul>
asked Feb 19, 2014 at 21:45
user2406735
2451 gold badge6 silver badges21 bronze badges
-
Why do you need the number in the array? isn't that covered by the index in the array?user400654– user4006542014年02月19日 21:54:40 +00:00Commented Feb 19, 2014 at 21:54
-
yes it is covered by index, you are right, that can be left out.user2406735– user24067352014年02月19日 21:59:01 +00:00Commented Feb 19, 2014 at 21:59
1 Answer 1
Something like this perhaps?
var result = [];
$('li').each(function(i){
var $img = $(this).find('img');
result.push([i+1, this.id, $img[0], $img.prop('src'), $img.prop('alt')]);
});
console.log(result);
I tried with map() at first, but it seems to flatten the array.
answered Feb 19, 2014 at 21:59
Johan
35.4k62 gold badges191 silver badges309 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
lang-js