7

I need to set the value of every item in this array, counting up.

So, for example, path[0].value = 1, path[1].value = 2 etc...

EDIT: I'm looking for the most efficient way to do this.

I think a for loop is the best way, but I want to learn other ways. Can it be done with the map() method or forEach()? What about a for... in statement? I'd like to do it with pure JS, but if you can teach me a better way with jQuery, I'd be interested to learn that too.

Thanks in advance.

function Cell(x,y){
    this.xCoordinate = x;
    this.yCoordinate = y;
    this.value;
}
var path = [new Cell(0,0), new Cell(0,1), new Cell(0,2)];
fatblacklip
  • 95
  • 1
  • 1
  • 6

4 Answers4

6

You can use a for loop or forEach:

for(var i=0; i<path.length; ++i)
  path[i].value = i+1;
path.forEach(function(cell, i) {
  cell.value = i + 1;
});

Better avoid for...in because of Why is using “for…in” with array iteration such a bad idea?.

Community
  • 1
  • 1
Oriol
  • 274,082
  • 63
  • 437
  • 513
1

If you have an existing array, you can use map.

var path = [0,1,2].map( x => new Cell(0, x))

or to mutate

path = path.map( x => {
  x.value = x.yCoordinate - 1
  return x 
}) 
Fresheyeball
  • 29,567
  • 20
  • 102
  • 164
  • The second is kind of just abusing `map` though. It may as well be `forEach` or `filter`. – Mike Cluck Mar 08 '16 at 22:35
  • I disagree, how is it abusing map? We are starting with one set, and then transforming it to a new set. Thats what map is for. – Fresheyeball Mar 08 '16 at 22:44
  • Prior to your edit, you were using `map` exclusively for it's ability to iterate over a set. Your most recent edit actually mutates it into an array of numbers. – Mike Cluck Mar 08 '16 at 22:48
  • Where is map useful? – fatblacklip Mar 09 '16 at 01:25
  • @Davita `map` is for creating a new set from an old set. For example, say you've got an array of numbers and you want to square them all. Then you could do `var squared = numbers.map(x => x * x)`. It creates a new array for each element in the original array, where the new value is what's returned from the function you passed it. – Mike Cluck Mar 09 '16 at 15:50
  • Map is a property of EndoFunctors such that `map (f . g) = map f . map g` which works on many more things than Arrays. For Arrays it is mostly for creating a new Array from a previous array based on a transformation. But it also useful for many more things. For example `(x => x + 4).map(x => x / 2)` should be possible and equal `(x => x + 4 / 2)` – Fresheyeball Mar 10 '16 at 21:22
0

A simple for loop should work:

var path = [],
    len = 10;

for (
  var idx = 0;
  idx < len;
  path.push(new Cell(0,++idx))
)
Malk
  • 11,855
  • 4
  • 33
  • 32
0
<html>
<body>
<p id="demo"></p>
<script>
function Cell(x,y){
    this.xCoordinate = x;
    this.yCoordinate = y;
    this.value;
}
function setValues(element, index, array){
array[index].value = index+1;
}
var path = [new Cell(0,0), new Cell(0,1), new Cell(0,2)];
path.forEach(setValues);
document.getElementById("demo").innerHTML = path[2].value;
</script>
</body>
</html>