0

I have the following issue for creating a object in Javascript

When a user clicks a button I want to push the sn_slab to the array slabs. But each serial_number is grouped by the batch_number.

The object should look something like this

    var Object = {
    'COFBP21018': {
        slabs: {
            0: 18765,
            1: 38947,
            ...
        }
    },
    'DEPOUS394O': {
        slabs: {
            0: 11006276,
            1: 11020446,
                    ...

        }
    },
    ..
}

my html looks like this

<a href=".." class="add_slab_to_array" data-batch_nr="COFBP21018" data-sn_slab="18765" />
<a href=".." class="add_slab_to_array" data-batch_nr="COFBP21018" data-sn_slab="38947" />
<a href=".." class="add_slab_to_array" data-batch_nr="DEPOUS394O" data-sn_slab="11006276" />
<a href=".." class="add_slab_to_array" data-batch_nr="DEPOUS394O" data-sn_slab="11020446" />

var block = {};
$('.add_slab_to_array').click(function(event) {
        event.preventDefault();

        var batch_nr = $( this ).attr('data-batch_nr');
        var sn_slab = $( this ).attr('data-sn_slab');

        // Create new object if batch_nr does not exists
        // ADD sn_slab to Array inside the Object with related batch_nr

      block[batch_nr] = {
           slabs: [],
           add: function(sn_slab) {
               this.slabs.push(sn_slab)
           }
       }

       block[batch_nr].add(sn_slab);

 });

The code above works, but my array slabs is always overridden.

5
  • Anchors are not self closing elements ? Commented Jun 10, 2014 at 14:19
  • And you realize that you're using the same key for several of those, so they will be overwritten when you use COFBP21018 twice etc. Commented Jun 10, 2014 at 14:20
  • 1
    @martinezjc - right smack in the middle of the code Commented Jun 10, 2014 at 14:21
  • Sorry i was watching first at the json object Commented Jun 10, 2014 at 14:21
  • Other than the mentioned problems, it works fine -> jsfiddle.net/8bvXz Commented Jun 10, 2014 at 14:22

2 Answers 2

2

It seems to me you're redefining the block object each time you click. You should instead check if it's set before proceeding.

block[batch_nr] = block[batch_nr] || {};

block[batch_nr].slabs = block[batch_nr].slabs || [];

block[batch_nr].add = block[batch_nr].add || function(sn_slab) {                         
            this.slabs.push(sn_slab);
           };
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think it needs to be this complicated... I think your diagnosis is correct, but I think the OP's code just needs the initial guard condition to prevent block[batch_nr] from being reinitialized
0

Consider this

     block[batch_nr] = {
       slabs: [],
       add: function(sn_slab) {
           if (block[batch_nr]) {
               block[batch_nr].slabs.push(sn_slab)
           }
       }
   }

1 Comment

Very insightful answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.