2

I am pushing a vector into array in the loop. Here is a code snippet

    var atoms = [];
    for (var i=0; i<formulas.length; i++) {
        for (var atom in formulas[i].data.atoms) {
            if (atoms.indexOf(atom) < 0) {
                atoms.push(atom);
            }
        }
    }
var zeroMatrix = [];
for (var i=0; i<formulas.length; i++) {
    var vector = atoms.map(function(item) {return formulas[i].data.atoms.hasOwnProperty(item) ? formulas[i].data.atoms[item] : 0});
    vector.push(0);
    console.log(vector);
    console.log(zeroMatrix);
    zeroMatrix.push(vector);
    console.log(zeroMatrix);
}

Results printed into the console are as follows

1) [1,2,0,0] // vector

2) [] // zeroMatrix

and now surprise

3) [[1,2,1,0],[2,0,1,0],[0,1,1,0]] // zeroMatrix after pushing the vector

Moreover, it pushes as expected if I run this in debug. Do you have any idea what can cause this behavior?

Mirek Surma
  • 148
  • 1
  • 10
  • Please show the actual loop. – Richard Kho Jul 14 '15 at 23:04
  • @RichardKho here you are. If you need any other info just let mi know. Thanks. – Mirek Surma Jul 14 '15 at 23:09
  • What does you `formulas` array look like – Jacob Carter Jul 14 '15 at 23:14
  • @JacobCarter it is an object of this structure {formula : 'FeS2', {data : {atoms : {Fe : 1, S : 2}} , molecularWeight : 123}} – Mirek Surma Jul 14 '15 at 23:18
  • and the `atoms` array you are referencing when you call `atoms.map()` – Jacob Carter Jul 14 '15 at 23:20
  • 1
    Fairly certain there's an issue here not represented in the code you shared. This works as expected with dummy data in your format. – nrabinowitz Jul 14 '15 at 23:26
  • You obviously messed up `[]` and `{}`. On object `{formula : 'FeS2', {data : {atoms : {Fe : 1, S : 2}} , molecularWeight : 123}}` can't be iterated in an Array manner using `for(var i=0...` and indexing `[i]` – Kirill Slatin Jul 14 '15 at 23:29
  • @nrabinowitz I am sure there is some bug in my code. However it seems to me really weird that it works perfectly in debug while I obtain this weird array when run normally. – Mirek Surma Jul 14 '15 at 23:30
  • @KirillSlatin actually the structure is right. The formula is an object ... formulas is an array of these objects – Mirek Surma Jul 14 '15 at 23:34
  • @nrabinowitz What I have showed is the whole body of given function. There is nothing more happening before (formulas are passed as parameter) – Mirek Surma Jul 14 '15 at 23:36
  • 2
    It happened to me that console.log would not reflect true state of objects sometimes. Or at least not at the precise moment the `console.log` was made. – Sebas Jul 14 '15 at 23:46
  • Sorry about the closure answer it should not be your issue. The console thing happens to me not very often, but I learned my lesson and stopped trusting it a while ago. – Sebas Jul 14 '15 at 23:57
  • The issue seems fixed, but try this: http://stackoverflow.com/a/4060176/1291428 – Sebas Jul 14 '15 at 23:59
  • @Sebas No problem, it was rather possible origin of this issue. I do not use JS very much so I still need to learn through mistakes like this. OK, lesson learnt ... do not trust the console :) – Mirek Surma Jul 14 '15 at 23:59
  • The other thing: did you try in different browsers? – Sebas Jul 15 '15 at 00:00
  • @Sebas I did now. It prints out [[1,2,1,0]] in internet explorer. In my opinion , there is a problem with an inner state of computation. – Mirek Surma Jul 15 '15 at 00:03
  • 1
    Try with `console.log(JSON.stringify(zeroMatrix))` – Sebas Jul 15 '15 at 00:04
  • 1
    @Sebas stringified object printed as expected ... doh. I have just spent 2 hours on fixing something what actually had worked :) Thank you for your time and very good inputs. – Mirek Surma Jul 15 '15 at 00:07

0 Answers0