-2

I'm trying to select every second element in the group of three. e.g.

[4, 5, 3, 2, 3, 1, 4, 5, 3]

Result: 5, 3, 5

How can I achieve this?

Michael Daineka
  • 143
  • 1
  • 2
  • 9
  • 4
    There are a number of ways to accomplish this but you should show what you have tried first. – Mike Cheel Jan 11 '19 at 16:11
  • 1
    That's every third number beginning with the second element. Should be easy to translate that into a `for` loop. – Andreas Jan 11 '19 at 16:13
  • 2
    Possible duplicate of [How to take every 3rd element of an array](https://stackoverflow.com/questions/41312888/how-to-take-every-3rd-element-of-an-array) –  Jan 11 '19 at 16:14

4 Answers4

3

You can use Array.prototype.filter and modulo operation:

const result = [4, 5, 3, 2, 3, 1, 4, 5, 3].filter((item, i) => (i+2) % 3 === 0);

console.log(result)
antonku
  • 7,377
  • 2
  • 15
  • 21
0

You have to split the array in chunks, then take the second element of every chunk:

/**
 * Returns an array with arrays of the given size.
 *
 * @param myArray {Array} array to split
 * @param chunk_size {Integer} Size of every group
 */
function chunkArray(myArray, chunk_size){
    var index = 0;
    var arrayLength = myArray.length;
    var tempArray = [];
    
    for (index = 0; index < arrayLength; index += chunk_size) {
        myChunk = myArray.slice(index, index+chunk_size);
        // Do something if you want with the group
        tempArray.push(myChunk);
    }

    return tempArray;
}
// Split in group of 3 items



function getResult () {
  // Outputs : [ [1,2,3] , [4,5,6] ,[7,8] ]
  var chunked = chunkArray([1,2,3,4,5,6,7,8], 3);
  var result = [];
  for(var i = 0; i < chunked.length; i++){
    result.push(chunked[i][1]);
  }
  return result;
  
}
console.log(getResult());

Chunking ref: https://ourcodeworld.com/articles/read/278/how-to-split-an-array-into-chunks-of-the-same-size-easily-in-javascript

Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
0
var arr = [4, 5, 3, 2, 3, 1, 4, 5, 3],
    filtered = arr.filter((val,idx) => idx % 3 == 1);
Omar Matijas
  • 71
  • 1
  • 6
  • Thanks for solution. Can you explain why is that? – Michael Daineka Jan 11 '19 at 19:37
  • Filter method, returns an array of each element which for the function return true. [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter] % is modulo operator, which return, in this case, the rest of dividing the index of each element by 3. So every three items you will have 0, 1 and 2 as the rest of the division and you only wanted the one in the middle. (Sorry about my english; spanish speaker). – Omar Matijas Jan 11 '19 at 21:01
0
//this function splits arrays into groups of n
function chunk(arr, limit) {
  var chunks = [];
  for (var i=0,len=arr.length; i<len; i+=limit)
    chunks.push(arr.slice(i,i+limit));
  return chunks;
}


var arr =  [4, 5, 3, 2, 3, 1, 4, 5, 3]

//split array in groups
var chunked = chunk(arr, 3);

//for each array in chunked array, do something with second item
chunked.forEach(function(i) {
    console.log(i[1]);
});

//You can also do:

chunked[2][1]
Penguin
  • 171
  • 1
  • 9