Javascript 数组值在循环内未定义
Javascript array value is undefined inside loop
我正在尝试比较两个对象。如果在 savedList 中也找到了 checkedList 中的项目,我想从 checkedList 中复制数组中的第一项,并将其与找到的项目放在 savedList 中。如果在 savedList 中找不到 checkedList 中的项目,我想将整个项目添加到 savedList。我不想在 savedList 中两次保留相同的项目。我正在使用 pop/push 方法来执行此操作。
我 运行 遇到的问题是在 [ while->for 循环] 中。
未捕获类型错误:无法读取未定义的属性(读取“1”)
当在 savedList 中找到一个项目时,我希望进行必要的更改,然后通过 pop() 移动到下一个项目。然而,
当我使用 pop() 方法时,前一项丢失了。下面是我的工作代码以及 link 到 JSFiddle.
期望输出:
savedItemList = {
"listName": "List_daily:Daily Get From Back List",
"date": "",
"itemList": [
[ 11, 888 ], [ 55, 227 ], [ 44, 236 ],
[ 3, 700 ], [ 2, 600 ]
]
};
var savedItemList = {
"listName": "List_daily:Daily Get From Back List",
"date": "",
//itemList contains [item quantity, item ID]
"itemList": [
[ 1, 888 ], [ 2, 227 ], [ 3, 236 ],
[ 3, 700 ], [ 2, 600 ]
]
};
var checkedItemList = [
//itemData contains [item quantity, item ID]
{ "rowNumber": 1, "itemData": [ 11, 888 ] },
{ "rowNumber": 4, "itemData": [ 44, 236 ] },
{ "rowNumber": 3, "itemData": [ 55, 227] }
];
function moveItemsToList(savedItemList, checkedItemList)
{
//while checkedItemList length is not zero
while(checkedItemList.length)
{
//pop the last element inside checkedList
var temp = checkedItemList.pop().itemData;
//loop through the items inside savedList
for(let k=0; k < savedItemList.itemList.length; k++)
{
//compare temp[1] itemID with savedList itemID
if(temp[1] == savedItemList.itemList[k][1])
{
//if found save the quantity from temp[0] variable to savedItemList
savedItemList.itemList[k][0] = temp[0];
//then move onto the next item on checkedList
temp = checkedItemList.pop();
}
}
//if items from CheckedList not Found in savedList //Add the item and its quantity to savedList
savedItemList.itemList.push(temp);
}
console.log(savedItemList);
}
moveItemsToList(savedItemList, checkedItemList);
使用现代 JavaScript 数组方法。
const savedItemList = {
"listName": "List_daily:Daily Get From Back List",
"date": "",
//itemList contains [item quantity, item ID]
"itemList": [
[ 1, 888 ], [ 2, 227 ], [ 3, 236 ],
[ 3, 700 ], [ 2, 600 ]
]
};
const checkedItemList = [
//itemData contains [item quantity, item ID]
{ "rowNumber": 1, "itemData": [ 11, 888 ] },
{ "rowNumber": 4, "itemData": [ 44, 236 ] },
{ "rowNumber": 3, "itemData": [ 55, 227] }
];
function moveCheckedToSaved(savedItemList, checkedItemList) {
checkedItemList.forEach((checkedItem) => {
const itemID = checkedItem.itemData[1]
const dupeIndex = findDuplicate(itemID, savedItemList)
if (dupeIndex !== -1) {
savedItemList[dupeIndex][0] = checkedItem.itemData[0]
} else {
savedItemList.push(checkedItem.itemData)
}
})
}
function findDuplicate(id, list) {
return list.findIndex(item => item[1] === id)
}
moveCheckedToSaved(savedItemList.itemList, checkedItemList)
document.write(savedItemList.itemList.join(' '))
我假设您想将数据保存在 checkedItemList
中,因为您说过 copy
。
下面的代码解决了你的问题,但是我重写了整个函数。
var savedItemList = {
"listName": "List_daily:Daily Get From Back List",
"date": "",
//itemList contains [item quantity, item ID]
"itemList": [
[ 1, 888 ], [ 2, 227 ], [ 3, 236 ],
[ 3, 700 ], [ 2, 600 ]
]
};
var checkedItemList = [
//itemData contains [item quantity, item ID]
{ "rowNumber": 1, "itemData": [ 11, 888 ] },
{ "rowNumber": 4, "itemData": [ 44, 236 ] },
{ "rowNumber": 3, "itemData": [ 55, 227] }
];
function moveItemsToList(savedItemList, checkedItemList){
//loop through checkItemList with checkedItem as the currently looped object
for(let checkedItem of checkedItemList){
itemData = checkedItem.itemData; //itemData is "itemData": [ quantity, ID ]"
//loop through savedItemList.itemList with savedItem as the currently looped array
for(let savedItem of savedItemList.itemList){
//check if the IDs between checkedItemList.itemData and savedItemList.itemList matched
if(itemData[1] === savedItem[1]){
//delete the matched data from savedItemList.itemList and break the savedItemList.itemList loop
savedItemList.itemList.splice(savedItemList.itemList.indexOf(savedItem), 1);
break;
}
}
//add new data to savedItemList.itemList
itemDataToBeAdded = itemData;
savedItemList.itemList.push(itemDataToBeAdded);
}
console.log(savedItemList);
}
moveItemsToList(savedItemList, checkedItemList);
编辑:将 for in
循环替换为 for of
循环,因为 for in
已弃用
我正在尝试比较两个对象。如果在 savedList 中也找到了 checkedList 中的项目,我想从 checkedList 中复制数组中的第一项,并将其与找到的项目放在 savedList 中。如果在 savedList 中找不到 checkedList 中的项目,我想将整个项目添加到 savedList。我不想在 savedList 中两次保留相同的项目。我正在使用 pop/push 方法来执行此操作。
我 运行 遇到的问题是在 [ while->for 循环] 中。
未捕获类型错误:无法读取未定义的属性(读取“1”) 当在 savedList 中找到一个项目时,我希望进行必要的更改,然后通过 pop() 移动到下一个项目。然而, 当我使用 pop() 方法时,前一项丢失了。下面是我的工作代码以及 link 到 JSFiddle.
期望输出:
savedItemList = {
"listName": "List_daily:Daily Get From Back List",
"date": "",
"itemList": [
[ 11, 888 ], [ 55, 227 ], [ 44, 236 ],
[ 3, 700 ], [ 2, 600 ]
]
};
var savedItemList = {
"listName": "List_daily:Daily Get From Back List",
"date": "",
//itemList contains [item quantity, item ID]
"itemList": [
[ 1, 888 ], [ 2, 227 ], [ 3, 236 ],
[ 3, 700 ], [ 2, 600 ]
]
};
var checkedItemList = [
//itemData contains [item quantity, item ID]
{ "rowNumber": 1, "itemData": [ 11, 888 ] },
{ "rowNumber": 4, "itemData": [ 44, 236 ] },
{ "rowNumber": 3, "itemData": [ 55, 227] }
];
function moveItemsToList(savedItemList, checkedItemList)
{
//while checkedItemList length is not zero
while(checkedItemList.length)
{
//pop the last element inside checkedList
var temp = checkedItemList.pop().itemData;
//loop through the items inside savedList
for(let k=0; k < savedItemList.itemList.length; k++)
{
//compare temp[1] itemID with savedList itemID
if(temp[1] == savedItemList.itemList[k][1])
{
//if found save the quantity from temp[0] variable to savedItemList
savedItemList.itemList[k][0] = temp[0];
//then move onto the next item on checkedList
temp = checkedItemList.pop();
}
}
//if items from CheckedList not Found in savedList //Add the item and its quantity to savedList
savedItemList.itemList.push(temp);
}
console.log(savedItemList);
}
moveItemsToList(savedItemList, checkedItemList);
使用现代 JavaScript 数组方法。
const savedItemList = {
"listName": "List_daily:Daily Get From Back List",
"date": "",
//itemList contains [item quantity, item ID]
"itemList": [
[ 1, 888 ], [ 2, 227 ], [ 3, 236 ],
[ 3, 700 ], [ 2, 600 ]
]
};
const checkedItemList = [
//itemData contains [item quantity, item ID]
{ "rowNumber": 1, "itemData": [ 11, 888 ] },
{ "rowNumber": 4, "itemData": [ 44, 236 ] },
{ "rowNumber": 3, "itemData": [ 55, 227] }
];
function moveCheckedToSaved(savedItemList, checkedItemList) {
checkedItemList.forEach((checkedItem) => {
const itemID = checkedItem.itemData[1]
const dupeIndex = findDuplicate(itemID, savedItemList)
if (dupeIndex !== -1) {
savedItemList[dupeIndex][0] = checkedItem.itemData[0]
} else {
savedItemList.push(checkedItem.itemData)
}
})
}
function findDuplicate(id, list) {
return list.findIndex(item => item[1] === id)
}
moveCheckedToSaved(savedItemList.itemList, checkedItemList)
document.write(savedItemList.itemList.join(' '))
我假设您想将数据保存在 checkedItemList
中,因为您说过 copy
。
下面的代码解决了你的问题,但是我重写了整个函数。
var savedItemList = {
"listName": "List_daily:Daily Get From Back List",
"date": "",
//itemList contains [item quantity, item ID]
"itemList": [
[ 1, 888 ], [ 2, 227 ], [ 3, 236 ],
[ 3, 700 ], [ 2, 600 ]
]
};
var checkedItemList = [
//itemData contains [item quantity, item ID]
{ "rowNumber": 1, "itemData": [ 11, 888 ] },
{ "rowNumber": 4, "itemData": [ 44, 236 ] },
{ "rowNumber": 3, "itemData": [ 55, 227] }
];
function moveItemsToList(savedItemList, checkedItemList){
//loop through checkItemList with checkedItem as the currently looped object
for(let checkedItem of checkedItemList){
itemData = checkedItem.itemData; //itemData is "itemData": [ quantity, ID ]"
//loop through savedItemList.itemList with savedItem as the currently looped array
for(let savedItem of savedItemList.itemList){
//check if the IDs between checkedItemList.itemData and savedItemList.itemList matched
if(itemData[1] === savedItem[1]){
//delete the matched data from savedItemList.itemList and break the savedItemList.itemList loop
savedItemList.itemList.splice(savedItemList.itemList.indexOf(savedItem), 1);
break;
}
}
//add new data to savedItemList.itemList
itemDataToBeAdded = itemData;
savedItemList.itemList.push(itemDataToBeAdded);
}
console.log(savedItemList);
}
moveItemsToList(savedItemList, checkedItemList);
编辑:将 for in
循环替换为 for of
循环,因为 for in
已弃用