在 Adonisjs 中循环数据时出现重复结果
Duplicate result when looping data in Adonisjs
我想 return 从一个数据 json 得到两个不同的双循环数据。但是循环的结果是一样的..这是我的输入,循环过程和输出:
"detail": [
{
"qty": 2,
"price": 1000,
"item_id": "1"
},
{
"qty": 5,
"price": 100000,
"item_id": "2"
}
]
这是创建 detail_id:
的第一个进程循环
const detail = request.input("detail");
const history = request.input("detail");
for (var i = 0; i < detail.length; i++) {
if(!detail[i].detail_id) {
detail[i].detail_id = nanoid(21);
}
console.log(detail[i]);
//output:
// [
// {
// "qty": 2,
// "price": 1000,
// "item_id": "1",
// "detail_id": "fDRS0NIaoNoS9kiauypjy"
// },
// {
// "qty": 5,
// "price": 100000,
// "item_id": "2",
// "detail_id": "fDRS0NIaoNoS9kiauypjy"
// }
//]
}
第二次循环创建history_id:
for (var x = 0; x < history.length; x++) {
if(!history[x].history_id) {
history[x].history_id = nanoid(21);
}
console.log(history[x]);
//output:
// [
// {
// "qty": 2,
// "price": 1000,
// "item_id": "1",
// "detail_id": "fDRS0NIaoNoS9kiauypjy"
// },
// {
// "qty": 5,
// "price": 100000,
// "item_id": "2",
// "detail_id": "fDRS0NIaoNoS9kiauypjy"
// }
//]
}
第二次循环的结果和第一次循环的结果一样,这段代码有什么问题?
const requestInput = [{
"qty": 2,
"price": 1000,
"item_id": "1"
},
{
"qty": 5,
"price": 100000,
"item_id": "2"
}
];
const detail = requestInput;
const history = requestInput;
function nanoid(id) {
return 'fDRS0NIaoNoS9kiauypjy';
}
for (var i = 0; i < detail.length; i++) {
if (!detail[i].detail_id) {
detail[i].detail_id = nanoid(21);
}
}
console.log(detail);
for (var x = 0; x < history.length; x++) {
if (!history[x].history_id) {
history[x].history_id = nanoid(21);
}
}
console.log(history);
您需要对传入的数据进行深度复制。现在,详细信息和历史记录都指向同一个对象。使用 [...data] 将创建一个浅表副本。这还不够,因为我们有嵌套数据。 Lodash 是一种流行的深度复制工具:https://lodash.com/
let data = [
{
qty: 2,
price: 1000,
item_id: "1"
},
{
qty: 5,
price: 100000,
item_id: "2"
}
];
// These are both referencing the same object
const detail = data;
const history = data;
for (var i = 0; i < detail.length; i++) {
if (!detail[i].detail_id) {
detail[i].detail_id = 10;
}
}
console.log(detail);
// The first loop is adding on detail_id
// [ { qty: 2, price: 1000, item_id: '1', detail_id: 10 },
// { qty: 5, price: 100000, item_id: '2', detail_id: 10 } ]
for (var x = 0; x < history.length; x++) {
if (!history[x].history_id) {
history[x].history_id = 10;
}
}
console.log(history);
// The second loop adds on history_id
// [ { qty: 2,
// price: 1000,
// item_id: '1',
// detail_id: 10,
// history_id: 10 },
// { qty: 5,
// price: 100000,
// item_id: '2',
// detail_id: 10,
// history_id: 10 } ]
我想 return 从一个数据 json 得到两个不同的双循环数据。但是循环的结果是一样的..这是我的输入,循环过程和输出:
"detail": [
{
"qty": 2,
"price": 1000,
"item_id": "1"
},
{
"qty": 5,
"price": 100000,
"item_id": "2"
}
]
这是创建 detail_id:
的第一个进程循环
const detail = request.input("detail");
const history = request.input("detail");
for (var i = 0; i < detail.length; i++) {
if(!detail[i].detail_id) {
detail[i].detail_id = nanoid(21);
}
console.log(detail[i]);
//output:
// [
// {
// "qty": 2,
// "price": 1000,
// "item_id": "1",
// "detail_id": "fDRS0NIaoNoS9kiauypjy"
// },
// {
// "qty": 5,
// "price": 100000,
// "item_id": "2",
// "detail_id": "fDRS0NIaoNoS9kiauypjy"
// }
//]
}
第二次循环创建history_id:
for (var x = 0; x < history.length; x++) {
if(!history[x].history_id) {
history[x].history_id = nanoid(21);
}
console.log(history[x]);
//output:
// [
// {
// "qty": 2,
// "price": 1000,
// "item_id": "1",
// "detail_id": "fDRS0NIaoNoS9kiauypjy"
// },
// {
// "qty": 5,
// "price": 100000,
// "item_id": "2",
// "detail_id": "fDRS0NIaoNoS9kiauypjy"
// }
//]
}
第二次循环的结果和第一次循环的结果一样,这段代码有什么问题?
const requestInput = [{
"qty": 2,
"price": 1000,
"item_id": "1"
},
{
"qty": 5,
"price": 100000,
"item_id": "2"
}
];
const detail = requestInput;
const history = requestInput;
function nanoid(id) {
return 'fDRS0NIaoNoS9kiauypjy';
}
for (var i = 0; i < detail.length; i++) {
if (!detail[i].detail_id) {
detail[i].detail_id = nanoid(21);
}
}
console.log(detail);
for (var x = 0; x < history.length; x++) {
if (!history[x].history_id) {
history[x].history_id = nanoid(21);
}
}
console.log(history);
您需要对传入的数据进行深度复制。现在,详细信息和历史记录都指向同一个对象。使用 [...data] 将创建一个浅表副本。这还不够,因为我们有嵌套数据。 Lodash 是一种流行的深度复制工具:https://lodash.com/
let data = [
{
qty: 2,
price: 1000,
item_id: "1"
},
{
qty: 5,
price: 100000,
item_id: "2"
}
];
// These are both referencing the same object
const detail = data;
const history = data;
for (var i = 0; i < detail.length; i++) {
if (!detail[i].detail_id) {
detail[i].detail_id = 10;
}
}
console.log(detail);
// The first loop is adding on detail_id
// [ { qty: 2, price: 1000, item_id: '1', detail_id: 10 },
// { qty: 5, price: 100000, item_id: '2', detail_id: 10 } ]
for (var x = 0; x < history.length; x++) {
if (!history[x].history_id) {
history[x].history_id = 10;
}
}
console.log(history);
// The second loop adds on history_id
// [ { qty: 2,
// price: 1000,
// item_id: '1',
// detail_id: 10,
// history_id: 10 },
// { qty: 5,
// price: 100000,
// item_id: '2',
// detail_id: 10,
// history_id: 10 } ]