0

I have different kind of javascript objects, they all have a property '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). In my case it's also possible to have 3 in a row, 5 in a row and so on..

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.

I've tried a bit in this JSFiddle: Here we can see the console output If you need more clarification, please ask.

Thanks in advance!

9
  • In your example, surely you should expect 4 lists out: 0-3, 5-7, 9-10 and 12? no? Commented Nov 28, 2013 at 11:45
  • 1
    Let me get this straight - do you want to do array slice of n elements, starting from the object that has the row property equal to certain value? Or do you want to do array chunk of n elements (then why the row property should matter at all)? Commented Nov 28, 2013 at 11:46
  • @Jamiec: I don't need to have objects 9, 10 and 12 because the sequence is incorrect. Commented Nov 28, 2013 at 11:49
  • @Eithedog: there are more properties than just 'row'. Each object has a different position in the row. I need to find the objects that are positioned next to each other. Commented Nov 28, 2013 at 11:50
  • They're all "next to each other" row1 has property row of 1, row2 has property row 2 etc.. What is your actual problem that you're trying to solve with this, as this as it stands make no sense? Commented Nov 28, 2013 at 11:51

3 Answers 3

1

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);

http://jsfiddle.net/B76a8/6/

Sign up to request clarification or add additional context in comments.

Comments

1

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/

output

[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=10}]

Comments

0

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:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.