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 ?

3
  • Why are you adding strings within each element? Also, look into for loops. Commented Feb 8, 2013 at 15:33
  • Are those objects always of the structure {x: 1, z: 2}? Commented Feb 8, 2013 at 15:33
  • 3
    Jeezus, so many people eager to make someone's homework. What have you tried? Commented Feb 8, 2013 at 15:38

7 Answers 7

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.

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

2 Comments

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.
@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?
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};
});  

6 Comments

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.
@Cerbrus: Also available in all other modern browsers and easy to shim for the older ones.
@ScottSauyet: Easy to shim, yes, but I think it's something that should be mentioned when using relatively new features.
Well, if this is indeed homework, I think his teacher expects him to use for :)
|
2
for (var i = 0; i < array.length; i++) {
    array[i]["x"] /= divisor;
    array[i]["y"] /= divisor;
} 

Comments

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

Comments

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

Comments

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

1 Comment

You forgot about array[i]["x"]
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;
    }

1 Comment

Could you please edit and elaborate your answer as to why OP or other people can try this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.