Calling an object of an array by the index in a loop

Asked

Viewed 61 times

1

I have this matrix:

r = {
    br : {x:2, y:0, z:4},
    pr : {x:2, y:1, z:5},
    ou : {x:1, y:1, z:6},
    pl : {x:1, y:1, z:7},
    di : {x:1, y:1, z:7}
    }

w = 0.5

And that tie that makes a calculation:

for (var t = 0; t < r.length; t++) {
    p = p + (r. *** .x * w) - (r. *** .y * (1-w));  
    console.log(p);
}

What’s the right shape on ***? I don’t know how to reference the object correctly with the traveled index. I am asking this, because for example if I call the object r.br.x it brings the result 2 (but I don’t know if there can that way either or the right way).

Do this with forEach would be more appropriate and better? If possible also someone could show me an example how would be in this case?

2 answers

1

Your object does not appear to be an array, since it has named attributes. Therefore to be able to stream it you will need to get your keys with the Object.keys or its values with Object.values and carrying out the calculation:

const calcular = (acumulador, x, y, w) => acumulador + (x * w) - (y * (1 - w));

const percorrer = (r, w) => Object.values(r).reduce((acumulador, { x, y }) => calcular(acumulador, x, y, w), 0);

Applying to your values:

const calcular = (acumulador, x, y, w) => acumulador + (x * w) - (y * (1 - w));

const percorrer = (r, w) => Object.values(r).reduce((acumulador, { x, y }) => calcular(acumulador, x, y, w), 0);

// Exemplo de chamada das funções
const r = {
  br : {x:2, y:0, z:4},
  pr : {x:2, y:1, z:5},
  ou : {x:1, y:1, z:6},
  pl : {x:1, y:1, z:7},
  di : {x:1, y:1, z:7}
};

const w = 0.5;

console.log(percorrer(r, w));

Object.keys

The method Object.keys() returns an array of enumerable properties of a given object in the same order it is provided by a loop for...in (the difference is that a for-in loop enumerates properties that are in the prototype chain).


Object.values

The method Object.values() returns an array with the properties values of a given object, in the same order provided by for...in loop (the difference being that the for-in loop also enumerates the properties in the prototypes string).


reduce

The method reduce() performs a function reducer (provided by you) for each member of the array, resulting in a single return value.

Example:

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15

0


What you have is not a matrix, but an object: a set of pairs "key/value", keyed ({ and }).

In this case, the keys are br, pr, etc. And the respective value of each of these keys is another object (whose keys are x, y and z, each with a numerical value).

To iterate through the object r, you can use Object.keys and then use a forEach to go through the keys. To access the value of each key, use brackets (r[chave]):

let r = {
    br : {x:2, y:0, z:4},
    pr : {x:2, y:1, z:5},
    ou : {x:1, y:1, z:6},
    pl : {x:1, y:1, z:7},
    di : {x:1, y:1, z:7}
};

let w = 0.5;
let p = 0;
Object.keys(r).forEach(k => {
    p = p + (r[k].x * w) - (r[k].y * (1-w));  
    console.log(p);
});

Although in this case you only seem to be interested in the value and not the key, then you can use Object.values, that gets each value directly:

let r = {
    br : {x:2, y:0, z:4},
    pr : {x:2, y:1, z:5},
    ou : {x:1, y:1, z:6},
    pl : {x:1, y:1, z:7},
    di : {x:1, y:1, z:7}
};

let w = 0.5;
let p = 0;
Object.values(r).forEach(v => {
    p = p + (v.x * w) - (v.y * (1-w));  
    console.log(p);
});


In Javascript there are no actual matrices, what is usually done is to use a array of arrays (albeit there are libraries to work with matrices).

  • Thank you very much! I wanted you to increment, is the logic of my code is wrong?

  • @Pedro The code increments the value of p, but if you do the table test will realize that when x and y are equal to 1, the value of (v.x * w) - (v.y * (1-w)) is equal to zero (the expression is (1 * 0.5) - (1 * (1 - 0.5)), which is zero). Then for the keys ou, pl and di (which have x and y equal to 1), the value added to p will be zero. If this is really the account you were supposed to do, then you’re right. Otherwise, just change the account, because the rest of the code (to go through all the values and add in p) that’s right

  • In fact, whenever x and y are equal, the value of (v.x * w) - (v.y * (1-w)) will be zero - it happens because w is 0.5 then 1 - w is also 0.5, so when x and y are equal, the expression is (valor * 0.5) - (valor * 0.5), which will always be zero. I don’t know if this is your problem, but finally, the code provided goes through all the values of the object and adds in p. Whether the accounts should go to zero or not is another story...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.