简体   繁体   English

从图像组填充二维数组

[英]Populating a two-dimensional array from image groups

I have several images, and I need to be able to group and select them by title, and also manipulate them individually. 我有几张图像,我需要能够按标题分组和选择它们,并分别进行操作。

My images look like this: 我的图像如下所示:

<img src="1.jpg" title="cat">
<img src="2.jpg" title="cat">
<img src="3.jpg" title="cat">
<img src="1.jpg" title="dog">
<img src="2.jpg" title="dog">
<img src="3.jpg" title="dog">
<img src="1.jpg" title="horse">
<img src="2.jpg" title="horse">
<img src="3.jpg" title="horse">

... and so on. ... 等等。

I'm trying to create an array that looks like this: 我正在尝试创建一个看起来像这样的数组:

imgArray = [cat[1,2,3], dog[1,2,3], horse[1,2,3]];

... where the first array level is the group(title), and within each group is an array of the actual elements with matching titles. ...,其中第一个数组级别是group(title),每个组中是带有匹配标题的实际元素的数组。 I need to be able to do things like this: 我需要能够执行以下操作:

var imgGroup = imgArray[cat];
doThings(imgGroup)
function doThings(target){
   var pri = target[0], sec = target[1], ter = target[2];
   pri.doThis(); sec.doThat(); ter.doTheotherthing();
});

So far I'm doing this: 到目前为止,我正在这样做:

img.each(function(){
   var self = $(this),  title = self.attr('title');
   imgArray.push([title, self]) 
   imgArray = $.unique(imgArray) //remove duplicate titles
});

But this is obviously not working and probably for a reason that makes me look stupid :/ 但这显然是行不通的,可能是由于某种原因使我看起来很愚蠢:/

Is anyone able to help me with this logic? 有谁能帮我这个逻辑吗?

I suspect the structure you really need is an object (not an array) where you have a property for each type of animal, and the value of each property is an array of numbers: 我怀疑您真正需要的结构是一个对象(而不是数组),其中每种动物都有一个属性,每个属性的值都是一个数字数组:

var imgObj = {
   "cat" : [1, 2, 3],
   "dog" : [1, 2, 3],
   "horse" : [1, 2, 3]
}

Then you can say: 然后您可以说:

imgObj["cat"]          // give the array [1,2,3]
imgObj["horse"].length // gives 3
imgObj["dog"][0]       // accesses the first value in the dog array

Except that the way your code is trying to create and use your array/object it seems you want the sub arrays to hold jQuery objects that themselves contain the individual DOM img elements? 除了您的代码尝试创建和使用数组/对象的方式之外,似乎您还希望子数组容纳本身包含单个DOM img元素的jQuery对象? Still you'd want an object where the properties are arrays. 仍然需要一个属性为数组的对象。

You didn't tag your question as jQuery, though from your existing code you seem to be using it, so here's how you could create the above struture from your markup: 您没有将问题标记为jQuery,尽管从现有代码中似乎正在使用它,所以这是从标记中创建上述结构的方法:

var imgObj = {};
$("img").each(function() {
   var title = this.title;
   if (!imgObj.hasOwnProperty(title))
       imgObj[title] = [];

   imgObj[title].push($(this));
});

You should then be able to call your existing doThings() function as follows: 然后,您应该能够如下调用现有的doThings()函数:

doThings(imgObj["cat"]);    // passes just the cat array

Why not use class names instead of titles? 为什么不使用类名而不是标题? Then you can handle all the 'horse' elements in a group, using getELementsdByClassName or a query that filters the images by class. 然后,您可以使用getELementsdByClassName或按类过滤图像的查询来处理组中的所有“马”元素。 Use the titles for unique information... 使用标题获取唯一信息...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM