简体   繁体   English

如何从数组中删除元素(包括下一个项目) - jquery

[英]How to remove elements from array(including next items) - jquery

I have the following arrays to start with: 我有以下数组开头:

var x = '[123_0|345_1|789_3|222_4|358_9]'

var y = '[345~2.0~/pathname.jpg|789~1.25~/pathname2.jpg|222~1.15~pathname3.jpg|090~1.0~/pathname4.jpg]'

What i need to do is: 我需要做的是:

Check if any of the items from x exist in y, then remove it (including the elements after the ~) 检查是否存在x中的任何项目,然后将其删除(包括〜之后的元素)

At the end i need an array as this: 最后我需要一个数组:

var newarray = [222~1.15~/pathname3.jpg|090~1.0~/pathname4.jpg]

I managed to remove the value x from array y by the following code: 我设法通过以下代码从数组y中删除值x:

var toremove = [];

var z = x.split('|')

$.each(z,function(){
     var newval= this.split('_')[0];
     toremove.push(newval);
 });

The above is returning(as expected): 以上是返回(如预期):

'[123,345,789,222,358]'

I can easily do an $.inArray to check and remove the above values in y.. But i need to remove the other values (from y) after the "~" as well 我可以轻松地执行$ .inArray来检查并删除y中的上述值。但是我需要在“〜”之后删除其他值(来自y)

Please help on how to achieve this 请帮忙说明如何实现这一目标

Its kinda strange what you want to do, but the important tool to use is the Array filter function, try the following and view in fiddler 它有点奇怪你想做什么,但使用的重要工具是Array filter功能,试试以下内容并在fiddler中查看

  var x = '[123_0|345_1|789_3|222_4|358_9]';

  var y = '[345~2.0~/pathname.jpg|789~1.25~/pathname2.jpg|222~1.15~pathname3.jpg|090~1.0~/pathname4.jpg]';


  x = x.substring(1, x.length-1); // remove [ and ]
  var needles = x.split('|')
  y = y.substring(1, y.length-1); // remove [ and 
  var haystack = y.split('|');
  var newArray = haystack.filter(function (value) {
      for (var ii=0; ii<needles.length; ii++) {
          var needle = needles[ii].split('_')[0];
          var needleRegex = new RegExp("^" + needle);
          if(value.match(needleRegex)) {
              return false;
          }
      }
      return true;
  });

  console.log(newArray);
  $('body').append(newArray.join('|'));

This assumes that you do in fact want to filter 222 , because 222_4 is in x ; 这假设你实际上想要过滤222 ,因为222_4x ;

http://jsfiddle.net/SLnV2/ http://jsfiddle.net/SLnV2/

Using regular expressions .. 使用正则表达式..

var x = '[123_0|345_1|789_3|222_4|358_9]',
    y = '[345~2.0~/pathname.jpg|789~1.25~/pathname2.jpg|222~1.15~pathname3.jpg|090~1.0~/pathname4.jpg]',
    pat = /(\d+)[^\]|]+\|?/, tmp = '', xx = x ;

while( (tmp = xx.match(pat)) ){
    if(y.indexOf(tmp[1]) != -1){
        y = y.replace(new RegExp(tmp[1]+"[^\\]|]+",'gi'), '');
    }
    xx = xx.replace(pat, '');
};

y = y.replace(/(\[)\|+|(.)\|+(?!\d)/g,'$1');
console.log(y); // prints =>  [090~1.0~/pathname4.jpg]

You can use delete to remove an element from an array but unfortunately it will leave a hole in the array. 您可以使用delete从数组中删除元素,但不幸的是它会在数组中留下一个洞。

You can use splice(index,number) : 你可以使用splice(index,number)

  1. index is the index of array index是数组的索引
  2. number` is how many element to delete number`是要删除多少元素

EDIT: 编辑:

example: 例:

numbers is an array as ['zero', 'one', 'sd', 'shi', 'go'] 数字是一个数组,如['zero', 'one', 'sd', 'shi', 'go']

using delete : 使用delete

delete numbers[2]
// numbers is ['zero', 'one', undefined, 'shi', 'go']

while if I use splice : 如果我使用splice

numbers.splice(2, 1);
// numbers is ['zero', 'one', 'shi', 'go']

try this , DEMO 试试这个, DEMO

$( document ).ready(function() {
    var x = '[123_0|345_1|789_3|222_4|358_9]';

    var y = '[345~2.0~/pathname.jpg|789~1.25~/pathname2.jpg|222~1.15~pathname3.jpg|090~1.0~/pathname4.jpg]';

    x=x.replace('[', '');
    var z = x.split('|');
    var toremove = [];
    $.each(z,function(){
        var newval= this.split('_')[0];
        toremove.push(newval);
    });

    y=y.replace('[', '');
    var zz = y.split('|');
    $.each(zz,function(){
        var newval= this.split('~')[0];
        var appendval= this;
        if(jQuery.inArray( newval, toremove ) != -1)
        {
            zz = jQuery.grep(zz, function(value) {
                return value != appendval;
            });
        }
    });
    alert(zz);
});

more detail about used function : grep , inArray 关于used函数的更多细节: grepinArray

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

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