Array formatting

Asked

Viewed 51 times

-2

Good morning, everyone! I have a question about formatting an array in javascript.

My code is that way.

var oidup = ['5006144', '5006146']; //valores oid especifica
var oids = ['1.3.6.1.2.1.31.1.1.1.18.', '1.3.6.1.2.1.10.127.1.1.4.1.5.']; //propriedades oid para metrica 
var oidtotal = []; // array

var j = 0;
var k = 0;
var z = 0;
for (j = 0; j < oids.length; j++) { 
    for (k = 0; k < oids.length; k++) {
        oidtotal[z] = [oids[j] + oidup[k]];
        z += 1;
    }
}

console.log(oidtotal);

it returns me the values as follows.

oidtotal = [
  [ '1.3.6.1.2.1.31.1.1.1.18.5006144' ],     
  [ '1.3.6.1.2.1.31.1.1.1.18.5006146' ],     
  [ '1.3.6.1.2.1.10.127.1.1.4.1.5.5006144' ],
  [ '1.3.6.1.2.1.10.127.1.1.4.1.5.5006146' ] 
]

I’m trying to format it so it would return me this way.

 oidtotal = ['1.3.6.1.2.1.31.1.1.1.18.5006144','1.3.6.1.2.1.31.1.1.1.18.5006146','1.3.6.1.2.1.10.127.1.1.4.1.5.5006144','1.3.6.1.2.1.10.127.1.1.4.1.5.5006146']

Can you help me to understand how I can do this?

I tried to create a new array, and use the Join method, but could not get the expected result. I tried to accomplish it that way.

formataroid[0] = oidtotal.join(['\',\'']);

But that was the result.

formtaroid = [
  "1.3.6.1.2.1.31.1.1.1.18.5006144','1.3.6.1.2.1.31.1.1.1.18.5006146','1.3.6.1.2.1.10.127.1.1.4.1.5.5006144','1.3.6.1.2.1.10.127.1.1.4.1.5.5006146"
]

I am trying to use the net-snmp module and the method session.get(formataroid, function(error, varbinds)

And it accepts string only. As it is in the doc https://www.npmjs.com/package/net-snmp

From now on, thank you very much.

  • 2

    Okay, we have the code, but could you please try to explain verbatim the purpose of the code? I can guess (and try to guess), but without a more formal description it’s kind of hard to be sure. Anyway, click [Edit] to add this information to your question.

  • 2

    Where is oidtotal[z] = [oids[j] + oidup[k]]; swap for oidtotal += oids[j] + oidup[k]; , I guess that’s it.

  • Good afternoon Luiz, I already put the description, thank you very much. Augusto, that way it doesn’t work, because it returns me without the apostrophe and the comma at the end of each Indice. Dessa forma me retorna assim:&#xA;&#xA;oidtotal = 1.3.6.1.2.1.31.1.1.1.18.50061441.3.6.1.2.1.31.1.1.1.18.50061461.3.6.1.2.1.10.127.1.1.4.1.5.50061441.3.6.1.2.1.10.127.1.1.4.1.5.5006146

1 answer

1

Thanks to all, I managed to solve, thanks to the suggestion of Augusto and I did some more increments and it worked. For anyone who needs help on the same subject as me, I’ll share the code.

var oidup = ['5006144', '5006146', ];
var oids = ['.3.6.1.2.1.31.1.1.1.18.', '.3.6.1.2.1.10.127.1.1.4.1.5.', '.3.6.1.2.1.31.1.1.1.1.', '.3.6.1.2.1.10.127.1.1.2.1.4.'];
var oidtotal = [];

var j = 0;
var k = 0;
var z = 0;
for (j = 0; j < oids.length; j++) {
    for (k = 0; k < oidup.length; k++) {
        oidtotal[z] = ['1'];
        oidtotal[z] += [oids[j] + oidup[k]];
        z += 1;
    }
}

console.log(oidtotal);

I removed the first character from the oids array and added it to the oidtotal array before the calculation so I do not run the risk of the array staying with Undefined and only after the values, and followed the suggestion of Augusto, but adding the Indice.

Thank you all.

Browser other questions tagged

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