如何设置标准化数据的数组索引 [英] How to set array index of normalized data

查看:110
本文介绍了如何设置标准化数据的数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试规范化数据集,更新数组索引,然后对数据进行规范化。

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

我想更改P.O。

数据模型如下:

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

预期输出:

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' 
    } 
  }
]

逐行记录时似乎已设置

一旦数据被更新,我想使用原始模式。

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

p>

我遇到的问题是更新逻辑将以相同顺序更改所有条目,而不仅仅是更新单行。 (即正在更新(set_no = 0,line_no = 1)(set_no = 0,line_no = 2),当它仅应更新第二种情况时。

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?

推荐答案

我认为问题出在这一行。

I think the issue is in this line.

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

将其更改为

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

原因:在Javascript中,对象是通过引用分配的,因此在两个项目中都引用了order对象,将其展开后,会将属性解压缩到由对象常量创建的新对象中,进行浅表复制。

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.

这篇关于如何设置标准化数据的数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆