0

I have written this code

var items = [
  [1, 2],
  [3, 4],
  [5, 6]
];

function listiteration(list, fromrows, torows, col) {

  var newl = list; //making a copy of list
  var iterla = [];
  for (i = fromrows; i <= torows; i++) {
    iterla[i] = [];
    for (j = col; j <= col; j++) {
      iterla[i][j] = newl[i][j];
    }
  }
  return iterla;
}
console.log(listiteration(items, 1, 2, 1));

result should be

[[4],[6]]

but getting

[ <1 empty item>, [ <1 empty item>, 4 ], [ <1 empty item>, 6 ] ]

how to solve this

1
  • 1
    what is the logic of getting [[4],[6]] Commented Jan 29, 2018 at 10:11

6 Answers 6

1

You don't need the second for loop. Just create a new list, iterate over the rows and use push function to add the current rows col-th element into the array.

const items = [[1, 2], [3, 4], [5, 6]];

function listiteration(list, fromRows, toRows, col) {

  const newList = [];
  
  for (let i = fromRows; i <= toRows; i++) {
     newList.push([list[i][col]]);
  }
  
  return newList;
}

const newItems = listiteration(items, 1, 2, 1);
console.log(newItems);

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

Comments

1

You can do this with slice() and map() methods.

var items = [[1, 2], [3, 4], [5, 6]];
function f(list, start, end, col) {
  return list.slice(start, end + 1).map(e => [e[col]])
}

const result = f(items, 1, 2, 1)
console.log(result)

You can also add check for arrays.

var items = [[1, 2], 'a', [5, 6], [1, 2]];
function f(list, start, end, col) {
  return list
    .slice(start, end + 1)
    .map(e => Array.isArray(e) ? [e[col]] : e)
}

const result = f(items, 1, 3, 1)
console.log(result)

Comments

0

A simpler approach

var items = [[1, 2], [3, 4], [5, 6]];

function listiteration(list, fromrows, torows, col) {
    var ret = [];
    for (var i = fromrows; i <= torows; i++) {
        ret.push(list[i][col]);
    }
    return ret;
}

console.log(listiteration(items, 1, 2, 1));

1 Comment

When you create a snippet, please make sure it runs without error
0

This would be the solution

var items = [ [1, 2], [3, 4], [5, 6] ];

function listiteration(list, start, end, col) {
  var result = [];
  for ( i = start; i <= end; i++ ) {
    result.push( [ list[ i ][ col ] ] )
  }
  return result;
}

console.log(listiteration(items, 1, 2, 1));

Comments

0

Other answers have given you different ways of doing this, and that's great.

But for learning purposes I've fixed the bug in the one you have done.

All you forgot to do was offset the arrays back to zero..

iterla[i - fromrows][j - col] = newl[i][j];

Example below..

var items = [
  [1, 2],
  [3, 4],
  [5, 6]
];

function listiteration(list, fromrows, torows, col) {

  var newl = list; //making a copy of list
  var iterla = [];
  for (i = fromrows; i <= torows; i++) {
    iterla[i - fromrows] = [];
    for (j = col; j <= col; j++) {
      iterla[i - fromrows][j - col] = newl[i][j];
    }
  }
  return iterla;
}
console.log(listiteration(items, 1, 2, 1));

Comments

0
var items = [
  [1, 2],
  [3, 4],
  [5, 6]
];

function listiteration(list, fromrows, torows, col) {
    var newArr    = list.splice(fromrows, torows);
    var newResult =[];
    newArr.map(value=>{
        newResult.push([value[col]]);
    });
    return newResult;
}

console.log(listiteration(items, 1, 2, 1));

Comments

Your Answer

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