[英]Javascript get object sequence out of array
I have different kind of javascript objects, they all have a property 'row'. 我有不同类型的javascript对象,它们都有一个属性'row'。
var row1 = {
row: 1
};
var row2 = {
row: 2
};
var row3 = {
row: 3
};
var row4 = {
row: 4
};
...
I have an array defined as follow: 我有一个数组定义如下:
var objArray = [];
In this array it's possible to have multiple 'rows'. 在这个数组中,可以有多个“行”。 The sequence is always te same starting from the lower row to a higher row. 从下排到上排,序列始终相同。
Now I want to get the objects that are linked next to each other (like 4 in a row). 现在我想得到彼此相邻的对象(比如连续4个)。 In my case it's also possible to have 3 in a row, 5 in a row and so on.. 在我的情况下,它也可以连续3个,连续5个,依此类推。
Example: 例:
objArray.push(row0);
objArray.push(row1);
objArray.push(row2);
objArray.push(row3);
objArray.push(row5);
objArray.push(row6);
objArray.push(row7);
objArray.push(row9);
objArray.push(row10);
objArray.push(row12);
In this case I need 2 lists, 1 containing row0 to 3 and one containing 5 to 7. 在这种情况下,我需要2个列表,1个包含row0到3,另一个包含5到7。
I've tried a bit in this JSFiddle: Here we can see the console output If you need more clarification, please ask. 我在这个JSFiddle中尝试了一下: 在这里我们可以看到控制台输出如果你需要更多说明,请询问。
Thanks in advance! 提前致谢!
I made a fiddle that does just this for you: 我为你做了一个小提琴:
for(var i = 0; i < objArray.length; i++) {
if(currentnum !== -1)
{
var result = objArray[i].row - currentnum;
currentnum = objArray[i].row;
if(result === 1)
{
currentarray.push(objArray[i]);
} else {
arrayofarrays.push(currentarray);
currentarray = [];
currentarray.push(objArray[i]);
}
} else {
currentnum = objArray[i].row;
currentarray.push(objArray[i]);
}
}
arrayofarrays.push(currentarray);
Since you have already figured how to keep the counter and reset it for each new group you can do 既然您已经想出如何保留计数器并为每个新组重置它,您可以这样做
var counter = 1,
lastIndex = 0;
//see chrome dev tools, I need to return objects beginning at the 3rd place untill the 7th place in the array
//how do I get these objects?
for (var i = 0; i < objArray.length; i++) {
if ((i < objArray.length - 1 && objArray[i].row + 1 == objArray[i + 1].row) ||
(i == objArray.length - 1 && objArray[i - 1].row == objArray[i].row - 1)) {
counter++;
} else {
// here we output the grouped items
console.log(objArray.slice(lastIndex, counter+lastIndex));
lastIndex = counter+lastIndex;
counter = 1;
}
}
Demo at http://jsfiddle.net/B76a8/7/ 演示http://jsfiddle.net/B76a8/7/
output 产量
[Object { row=0}, Object { row=1}] [Object {row = 0},Object {row = 1}]
[Object { row=3}, Object { row=4}, Object { row=5}, Object { row=6}, Object { row=7}, Object { row=8}] [Object {row = 3},Object {row = 4},Object {row = 5},Object {row = 6},Object {row = 7},Object {row = 8}]
[Object { row=10}] [对象{row = 10}]
First, let's sort the array: 首先,让我们对数组进行排序:
objArray.sort(function(a,b){ return a.row - b.row });
Then, for the given n, this should return you next and previous elements: 然后,对于给定的n,这应该返回下一个和前一个元素:
function getElement(array, n)
{
var ret = [];
for (var i=0; i<array.length; i++)
if (array[i].row == n)
{
if (i > 0) ret.push(array[i-1]);
if (i < array.length-1) ret.push(array[i+1]);
}
return ret;
}
As getting all the other options with the same color is a different thing let's do it through: 由于使用相同颜色获得所有其他选项是不同的事情,让我们通过:
function getByColor(array, color)
{
var ret = [];
for (var i=0; i<array.length; i++)
if (array[i].color == color)
ret.push(array[i]);
return ret;
}
Then you can merge both of the arrays by using concat
然后,您可以使用concat
合并两个阵列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.