3

I have an array called fillet_array, which contains objects (currently there is only one object, but this changes throughout the program). This is the output in Chrome:

0: Object
  cid: "9351"
  imgsrc: "1a4b1ebb4a31e2104ec1f2bab8539731"
  index: 0
  sku: "TD00060S0"
  width: 0.31

This is my code (Note at this time mattes_number_layers is 2):

console.log(fillet_array);
  if ("undefined" !== typeof(fillet_array) && "undefined" !== typeof(fillet_array[mattes_number_layers - 2]))
  {
    console.log("mattes_number_layers - 2: " + (mattes_number_layers - 2)); //outputs 0
    console.log("mattes_number_layers - 1: " + (mattes_number_layers - 1)); //outputs 1
    console.log(fillet_array[mattes_number_layers - 2]); //outputs Object
    console.log(fillet_array[mattes_number_layers - 2].index); //outputs 0
    fillet_array[mattes_number_layers - 2]['index'] = (mattes_number_layers - 1);//switch index in fillet_array
  } 
  console.log(fillet_array);

I am trying to change the index property of the object from 0 to 1, but the code I have does not seem to be working because when I output fillet_array again, it is the same as the first one.

Edit: Here is the code creating the fillet object and adding it to the array. This is called before the code above.

var fillet = {};
fillet.index = index;
fillet.imgsrc = thumb;
fillet.width = width;
fillet.cid = cid;
fillet.sku = sku;

if (typeof(fillet_array) === "undefined")
{
  fillet_array = [];
}

fillet_array[index] = fillet; //For now index is 0
AllisonC
  • 2,973
  • 4
  • 29
  • 46
  • Have you tried using . notation it shouldn't make a difference but you could try it... `fillet_array[mattes_number_layers - 2].index = (mattes_number_layers - 1);` – brso05 Nov 11 '14 at 14:20
  • Yes I have tried that as well, but it did not work – AllisonC Nov 11 '14 at 14:20
  • Where is this code located? Can you give more context... – brso05 Nov 11 '14 at 14:24
  • 1
    Your example code works correctly in Firefox 33. Index is updated from 0 to 1 when fillet_array contains the single example object. Providing more context might be necessary. – user3357118 Nov 11 '14 at 14:29
  • It also works in Google Chrome 38 – Markai Nov 11 '14 at 14:32
  • works for me. Here's a fiddle: http://jsfiddle.net/m635nzrk/ Can you reproduce it here? – DanielST Nov 11 '14 at 14:41
  • @slicedtoad - the first output of fillet_array has the index of 1. – AllisonC Nov 11 '14 at 14:45
  • 1
    I closed the question as duplicate of the problem I believe you are experiencing. Happy to reopen if it's not. Actually, this one might be better: http://stackoverflow.com/q/4057440/218196 – Felix Kling Nov 11 '14 at 14:46
  • @AllisonC Felix is right, the problem is the async console log. If you comment out everything below the first `console.log(fillet_array);` the index is 0. – DanielST Nov 11 '14 at 14:50
  • 1
    use `console.log(JSON.stringify(obj))` like here: http://jsfiddle.net/m635nzrk/2/ to get the right results. – DanielST Nov 11 '14 at 14:55
  • Yes I have discovered this to be the case. Thank you for pointing me in the correct direction. :) – AllisonC Nov 11 '14 at 15:02

0 Answers0