Я хочу вернуть два разных данных с двойным циклом из одного 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);