Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery pop从末尾删除该项_Javascript_Jquery - Fatal编程技术网

Javascript jQuery pop从末尾删除该项

Javascript jQuery pop从末尾删除该项,javascript,jquery,Javascript,Jquery,我有这个问题是关于 . 这个很好用。我修改了它以符合我的要求。我的javascript如下所示。 当复选框被选中时,我希望被添加到相应的ID中(工作正常),但当复选框被取消选中时,我希望它被删除。因此我使用了pop。这是可行的,但它删除了最后一个项目ID,而不是未检查的项目ID ` var arrayPgggoData = pgggoThisElem.parents('.elementor-widget-container').find('.pgggo-sort-container').at

我有这个问题是关于 . 这个很好用。我修改了它以符合我的要求。我的javascript如下所示。 当复选框被选中时,我希望被添加到相应的ID中(工作正常),但当复选框被取消选中时,我希望它被删除。因此我使用了
pop
。这是可行的,但它删除了最后一个项目ID,而不是未检查的项目ID

`  var arrayPgggoData = pgggoThisElem.parents('.elementor-widget-container').find('.pgggo-sort-container').attr('data-ajax-container');
    var arrayPgggoData = JSON.parse(arrayPgggoData);
    if ($(this).is(':checked')) {
      const [_, taxonomy, __, attr] = $(this).attr('name').split('-');
      arrayPgggoData[taxonomy] = arrayPgggoData[taxonomy] || [];
      arrayPgggoData[taxonomy].push(attr.split('[')[0]);
    } else {
      const [_, taxonomy, __, attr] = $(this).attr('name').split('-');
      arrayPgggoData[taxonomy] = arrayPgggoData[taxonomy] || [];
      arrayPgggoData[taxonomy].pop(attr.split('[')[0]);
    }`

有人能帮我吗|

pop函数按预期的方式运行,基本上删除了您拥有的最后一个元素

这里需要的是
拼接(开始,位置)
,但在这种情况下,需要找到元素出现的位置。为此,您需要使用
indexOf(element)

你需要这样的东西:

var指数=indexOf(元素);
arrayPgggoData.拼接(索引1);

您可以查看有关JavaScript中从数组中删除元素的不同方法的详细信息。

pop()
不接受参数。它总是删除最后一个元素
.pop()
正在按预期工作。
pop()
方法从数组中删除最后一个元素并返回该元素。此方法会更改数组的长度。您希望
.slice()
splice()
对数组进行变异original@Taplar我不能使用拼接或切片,因为我只有一个值。你是对的,我修复了那个部分<代码>切片()也可以。我不能使用拼接或切片,因为我只有值。您必须首先使用
indexOf(value)
找到值的索引,然后使用
splice()
slice()
与该索引一起使用。