简体   繁体   English

javascript从数组中删除项目,拼接

[英]javascript removing item from an array, splice

I'm making a small appwith localstaorage (not implemented yet): you type a note int the text area and it is display in a list 我正在使用本地存储制作一个小应用程序(尚未实现):您在文本区域输入注释,它会显示在列表中

the note are stacked in an object called notes (for localstorage in the future); 便笺被堆叠在一个称为便笺的对象中(将来用于本地存储);

But my problem is : I can add a note, but when I try to remove on of them, I have to remove my li and the related note object in the 'notes' array, so i decided to use splice method, but it works in a strange way... 但是我的问题是:我可以添加一个便笺,但是当我尝试删除它们时,我必须删除'notes'数组中的li和相关的便笺对象,因此我决定使用splice方法,但是它可以工作以一种奇怪的方式

when i click 'close', it works fine one or two times but at a moment the array stays with one or two object in it... 当我单击“关闭”时,它可以正常工作一到两次,但此刻数组会停留在其中一两个对象...

I tried different ways to solve the problem but without success... 我尝试了不同的方法来解决问题,但没有成功...

Here is the fiddle : http://jsfiddle.net/h8hg6/1/ 这是小提琴: http : //jsfiddle.net/h8hg6/1/

thanks for your help 谢谢你的帮助

I have made some modifications to your fiddle that I think solve the problem. 我对您的提琴进行了一些修改,以解决问题。 Essentially you were using .splice incorrectly, and your Array was falling out of sync with your note elements. 实质上,您错误地使用了.splice ,并且数组与您的note元素不同步。 I've replaced your array with an numeric-based object because it is much easier to deal with. 我已将数组替换为基于数字的对象,因为它更容易处理。 Here are some of the relevant changes: 以下是一些相关更改:

http://jsfiddle.net/h8hg6/2/ http://jsfiddle.net/h8hg6/2/

var notes = {}; // notes is now an object instead of an array
// snip

var number = jQuery(this).parents('li').data('number');
delete notes[number]; // this is how you remove properties from an object

// snip
var note = {
    color: color,
    text: text
};

notes[i] = note; // add this object as a property of the notes object
i++;

The problem is that your call to splice uses the value of the queue variable to determine the index of the element that will be removed in the notes array. 问题在于您对splice的调用使用queue变量的值来确定将在notes数组中删除的元素的索引。 Right here: 就在这儿:

notes.splice(queue, 1);

Since the queue value is always increasing (right here): 由于queue值总是在增加(就在这里):

 function addNoteToPage(){
    i++;
    ...
    jQuery('ul#notes li:first').before('<li  data-queue="'+ i +'">'+ note.text +' <a href=""#>CLOSE</a>

You hit a moment where you call splice on an non-existing index of the notes array and nothing is removed as a result. 您碰到了一会儿,您在notes数组的不存在的索引上调用拼接,结果一无所获。 Basically, you end up with an out-of-sync notes array. 基本上,您最终会获得不同步的notes数组。

You need to make sure that the value of the data-queue attribute coincides with the real index of the element in the notes array so that your call to splice(queue,1) always succeeds and removes the appropriate array element. 您需要确保data-queue属性的值与notes数组中元素的实际索引一致,以便对splice(queue,1)调用始终成功并删除相应的数组元素。

With that said, if above answer works for you, I'd go with that one. 话虽如此,如果上面的答案对您有用,我会同意的。 I just wanted to give you more insight on what was going on... 我只是想让您对正在发生的事情有更多的了解...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM