0

I need a simple way to divide all the numbers (contained in objects) in an array by a variable in javascript (no jquery or other libraries) :

var divisor = 16;
var array = [{"x":0,"y":16},{"x":16,"y":32},{"x":32,"y":48}];

Expected Result:

[{"x":0,"y":1},{"x":1,"y":2},{"x":2,"y":3}];

Any ideas ?

Julian
  • 698
  • 2
  • 8
  • 17

7 Answers7

5

This should do it for you:

var divisor = 16;
var array = [{"x":0,"y":16},{"x":16,"y":32},{"x":32,"y":48}];

for(var i = 0, length = array.length; i < length; i++){
    array[i] = {'x':array[i].x/divisor,'y':array[i].y/divisor};
}

In case you are likely to extend the objects in the future, you might want to do it like this, instead:

for(var i = 0, length = array.length; i < length; i++){
    array[i].x /= divisor;  // `a[i].x /= d` is shorthand for `a[i].x = a[i].x / d`
    array[i].y /= divisor;
}

This has the advantage that it doesn't overwrite array[i], saving possible other properties.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • This can be a problem in the future if the dictionaries gain more keys, like *speed* or something. Suddendly they get removed with your code. – Georg Schölly Feb 08 '13 at 15:43
  • @GeorgSchölly: that's out of the scope of the question, imo, but I've edited in another option. If the downvote's yours, could you consider removing it? – Cerbrus Feb 08 '13 at 15:47
3

Another solution, using map and a callback:

var divisor = 16;
var array = [{"x":0,"y":16},{"x":16,"y":32},{"x":32,"y":48}];

array = array.map(function(v){
  return {x: v.x / divisor, y: v.y / divisor};
});  
nice ass
  • 16,471
  • 7
  • 50
  • 89
  • Keep in mind that [Array.prototype.map() is IE9+](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map#Browser_compatibility) – Cerbrus Feb 08 '13 at 15:38
  • You should either use the `forEach` method or don't modify `v`. It works in this example but `map` is not supposed to modify the original objects. – Georg Schölly Feb 08 '13 at 15:42
  • @Cerbrus: Also available in all other modern browsers and easy to shim for the older ones. – Scott Sauyet Feb 08 '13 at 15:50
  • @ScottSauyet: Easy to shim, yes, but I think it's something that should be mentioned when using relatively new features. – Cerbrus Feb 08 '13 at 15:53
  • Well, if this is indeed homework, I think his teacher expects him to use `for` :) – nice ass Feb 08 '13 at 15:53
  • @OneTrickPony: But a teacher would be impressed with a student who could use **and explain** `map` instead! – Scott Sauyet Feb 08 '13 at 15:58
2
for (var i = 0; i < array.length; i++) {
    array[i]["x"] /= divisor;
    array[i]["y"] /= divisor;
} 
Marek Musielak
  • 26,832
  • 8
  • 72
  • 80
2

Deep divide here.

var divideBy = function(object, divider) {
    if (typeof(object) == 'number') return object/divider;
    for (var i in object) {
        if (object.hasOwnProperty(i)) {
            object[i] = divideBy(object[i], divider);
        }
    }
    return object;
}

var obj = [{a: 16, b: 32}, {c: 0, d: 48, e: 160}];
console.log(divideBy(obj, 16));
Pythonic
  • 486
  • 5
  • 9
1

Here it is as a function so you can re-use it. It is not limited to just 2 variables per object.

function divideArray(array, divisor) {
    var i = array.length, a, k;
    while (i) { // loop over each item in array
        a = array[--i];
        for (k in a) { // loop over each key in object
            if (a.hasOwnProperty(k)) { // ignore inherited keys
                a[k] = a[k] / divisor; // calculate
            }
        }
    }
    return array;
}

// use with your example
var divisor = 16;
var array = [{"x":0,"y":16},{"x":16,"y":32},{"x":32,"y":48}];
array = divideArray(array, divisor);
Paul S.
  • 64,864
  • 9
  • 122
  • 138
0

Try this

        var divisor = 16;
        var array = [{"x":0,"y":16},{"x":16,"y":32},{"x":32,"y":48}];
        var i=0;
        for(i=0;i<array.length;i++)
        {
            array[i]["y"]=array[i]["y"]/divisor;
            array[i]["x"]=array[i]["x"]/divisor;
        }
iJade
  • 23,144
  • 56
  • 154
  • 243
0
var divisor = 16;
    var array = [{"x":0,"y":16},{"x":16,"y":32},{"x":32,"y":48}];
    var i=0;
    for(i=0;i<array.length;i++)
    {
        array[i]["y"]=array[i]["y"]/divisor;
        array[i]["x"]=array[i]["x"]/divisor;
    }