1

I am trying to normalize a dataset, update an array index, and then denormalize the data.

I'd like to change a P.O. at on a header line and have the change propagate to a linked order.

The data model is as follows:

let numSet = 0;
let numLine = 2;

let data = [
  {
    "order": {
      "po_no": "original-po"
    },
    "items": [
      {
        "header": {
          "po_no": "keep-this-value",
          "set_no": 0
        },
        "line": {
          "id": "A123",
          "line_no": 1
        }
      },
      {
        "header": {
          "po_no": "update-with-this-value",
          "set_no": 0
        },
        "line": {
          "id": "B234",
          "line_no": 2
        }
      }
    ]
  }
];

// The logic to normalize the data (appending the order data to each index), works as expected

let normalizedDataSet = [];
for (let i = 0; i < data.length; i++) {
  for (let j = 0; j < data[i]['items'].length; j++) {
    data[i]['items'][j]['order'] = data[i]['order']; // Set default header
    normalizedDataSet.push(data[i]['items'][j]);
  }
}

// The logic to update the normalized data, updating too many indices

for (i = 0; i < normalizedDataSet.length; i++) {
  let index = normalizedDataSet[i];

  if (numSet === index['header']['set_no'] && numLine === index['line']['line_no']) {
    index['order']['po_no'] = index['header']['po_no'];
  }
}

console.log(normalizedDataSet); // Expected output below

Expected output:

normalizedDataSet = [
  { 
    "header": { 
      "po_no": 'keep-this-value', 
      "set_no": 0 
    },
    "line": { 
      "id": 'A123', 
      "line_no": 1 
    },
    "order": { 
      "po_no": 'original-po' 
    } 
  },
  { 
    "header": { 
      "po_no": 'update-with-this-value', 
      "set_no": 0 
    },
    "line": { 
      "id": 'B234', 
      "line_no": 2 
    },
    "order": { 
      "po_no": 'update-with-this-value' 
    } 
  }
]

When logging line-by-line it seems to set correctly, but then there is a glitch when logging after the second for loop exits.

Once the data is updated I would like to resort it with the original schema.

The issue that I'm having is that the update logic is changing all entries with the same order, and is not just updating the single line. (i.e., it is updating (set_no = 0, line_no = 1) and (set_no = 0, line_no = 2), when it should only be updating the second case.

In this case, how would I update just the second index of the normalized data set?

3
  • I think you should add the expected output too. For example I can not really track where the "main" po_no should go, if anywhere. Commented Jun 12, 2019 at 14:25
  • OK. It is updated. The header po_no should set the order po_no when the set_no and line_no match the input. Commented Jun 12, 2019 at 14:35
  • I'd then like denormalize back to the original schema after this update step. Commented Jun 12, 2019 at 14:37

1 Answer 1

1

I think the issue is in this line.

data[i]['items'][j]['order'] = data[i]['order']; // Set default header

change this to

data[i]['items'][j]['order'] = { ...data[i]['order'] }; // Set default header

Reason: In Javascript the objects are assigned by reference. Thus the order object was referenced in both the items. When you spread it, then it unpacks the properties into the new object created by the object literal, making a shallow copy.

Sign up to request clarification or add additional context in comments.

3 Comments

This worked, although I'm unfamiliar with ... syntax and copy by reference. Is there a resource you could point me to, to learn more about this?
These are great. Thanks again!!

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.