Estoy tratando de comparar dos objetos. Si un elemento dentro de la lista marcada también se encuentra dentro de la lista guardada, me gustaría copiar el primer elemento de la matriz de la lista marcada y colocarlo con el elemento encontrado en la lista guardada. Si los elementos dentro de la lista marcada no se encuentran en la lista guardada, quiero agregar el elemento completo a la lista guardada. No deseo mantener el mismo elemento dos veces dentro de la lista guardada. Estoy usando el método pop/push para hacer esto.
El problema con el que me estoy encontrando está dentro del bucle [while->for].
TypeError no capturado: no se pueden leer las propiedades de undefined (leyendo '1') Cuando se encuentra un elemento dentro de la lista guardada, deseo realizar la modificación necesaria y luego pasar al siguiente elemento a través de pop (). Sin embargo, cuando uso el método pop(), el elemento anterior se pierde. A continuación se muestra mi código de trabajo, así como un enlace a JSFiddle .
Salida deseada :
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);Utilice métodos modernos de matriz de 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(' '))Supongo que desea mantener los datos dentro checkedItemList ya que dijo copy . El siguiente código resuelve su problema; sin embargo, he reescrito toda la función.
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); EDITAR: reemplazado for in loop con for of loop porque for in está en desuso