Filter n arrays by excluding values ​​that do not exist in each

I have n arrays with which I need to determine if x is in all n arrays. (where n is any number and x is a numerical value). I have something like the following in place, but it always ends up being false.

function filterArrays()
{
  var x = $(this).attr('id'); // ex: 2
  var arrays = [[1,2,3],[2,4,6]];
  var result = false;
  for each (var n in arrays)
  {
    result = result ^ (n.indexOf(x) > -1);
  }
}

How to make resultequal truewhen it xis in both arrays, but when xit is not in both arrays, do resultequal false?

The function above will be used with the jQuery method filter(). Example:

$(arrayOfElementsWithNumericIds).filter(arrayFilter);
// arrayOfElementsWithNumericIds prototype: [div#1,div#2,div#3,...]

I think bitwise operation is required, but I could be wrong. Please explain why your decision is correct and why mine is not working. (for bonus points)

+3
source share
6

:

  • (id - ). x = parseInt(...)
  • ^. result true &&.
  • each. for (key in object)

:

function filterArrays()
{
    var x = parseInt($(this).attr('id')); // ex: 2
    var arrays = [[1,2,3],[2,4,6]];
    var result = true;
    for (var n in arrays)
    {
        result = result && (arrays[n].indexOf(x) > -1);
    }
    return result;
}

, Array.every() Array.some(). , $(this).attr('id') jQuery , this.id .

function filterArrays()
{
    var x = parseInt(this.id); // ex: 2
    var arrays = [[1,2,3],[2,4,6]];
    var result = arrays.every(function(array)
    {
        return array.some(function(item)
        {
            return item === x;
        });
    });
    return result;
}
+3

, :

  var result = true;
 for each (var n in arrays)
  {
    result = result && (n.indexOf(x) > -1);
  }

, , . , AND (&),

  true AND (value is in current array)

, , . .

+1

xor . :

search for 2, start  result = false
1st array: 2 is present, result = false xor true = true
2nd array: 2 is present, result = true xor true = false
end: result is false (WRONG)

search for 4, start result = false
1st array: 4 is present, result = false xor true = true
2nd array: 4 is  absent, result = true xor false = true
end: result is true (WRONG)

- .

start: result = true, search for 2
1st array: 2 is present, result = true and true = true
2nd array: 2 is present, result = true and true = true
end: result is true (RIGHT)

start: result = true, search for 4
1st array: 4 is present, result = true and true = true
2nd array: 4 is absent, result = true and false = false
end: result if false (RIGHT)
+1

contains? , / .

0

, "", , , Set Intersect. jQuery, , attr (id) x . , ...

function filterArrays(){
  var x = $(this).attr("id");
  var arrays = [[1,2,3],[2,4,6]];
  var result = ($.inArray(arrays[0], x )>0 && $.inArray(arrays[1], x) >0);
  return result;
}
0

http://phrogz.net/JS/ArraySetMath.js, :

var sets  = [[1,2,3],[2,3,7],[1,7,2]];
var isect = sets[0];
for (var i=1,len=sets.length;i<len;++i){
  isect = isect.intersection( sets[i] );
}
console.log( isect );
// [2]

, JS.Set, :

var sets  = [[1,2,3],[2,3,7],[1,7,2]];

// Or JS.HashSet or JS.Set
var isect = new JS.SortedSet(sets[0]);
for (var i=1,len=sets.length;i<len;++i){
  isect = isect.intersection( new JS.SortedSet(sets[i]) );
}
0

Source: https://habr.com/ru/post/1787762/


All Articles