Check if partial value is inside array

MichaelR

I have this array :

>> arr = [["a",1,"b"],["a",2,"b"],["b",3,"d"],["t",6,"a"]]

I want to check if ["a",1] exists inside the arr while ignoring the 3rd value of the arr items.

Is there a better way then first removing the 3rd value from each item in arr:

>> new_arr = [["a",1],["a",2],["b",3],["t",6]]

and then doing

>> new_arr.include? ["a",1]
true

Something like:

arr.include? ["a",1,/*/]  #the 3rd value can be anything

Thanks.

Arup Rakshit

You can try the below :

arr = [["a",1,"b"],["a",2,"b"],["b",3,"d"],["t",6,"a"]]
arr.any? { |v1, v2, *| [v1, v2] == ["a", 1] }
# => true
arr.any? { |v1, v2, *| [v1, v2] == ["a", 4] }
# => false

wrap the logic inside a method :

def check_subarray(ary, sub_ary)
  ary.any? { |e1, e2, *| [e1, e2] == sub_ary }
end

arr = [["a",1,"b"],["a",2,"b"],["b",3,"d"],["t",6,"a"]]
check_subarray(arr, ["a", 1]) # => true

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fast way to check if partial value is present in large array of hashes

From Dev

$.inArray() : check if array value has "+" inside

From Dev

$.inArray() : check if array value has "+" inside

From Dev

how to check if a value inside an array is larger than a certain value

From Dev

Check each value inside a nested array of a dictionary Swift

From Dev

How to match letters and check that value is present inside array or not using PHP

From Dev

Check value inside Map

From Dev

pass a parameter to html partial and retrieve value inside the partial html

From Dev

Check / match string against partial strings in array

From Dev

Array inside Array echoing value not inside array

From Dev

Check if value exists in multidimensional array and edit other value inside same key

From Dev

How to check the value is present inside an array and insert the value into database using PHP

From Dev

check value inside a link javascript

From Dev

Searching for a value inside an array

From Dev

Check if string is in array inside dictionary

From Dev

How to check if inside an array PHP

From Dev

Check input value in array

From Dev

jQuery check if value not in array

From Dev

Check if an array has a value

From Dev

How to check for a value in an array

From Dev

Check if an array value is set or not

From Dev

Android find array index value of a partial match

From Dev

Android find array index value of a partial match

From Dev

Linq Check if array of string has a partial match in other array

From Dev

Linq Check if array of string has a partial match in other array

From Dev

Check if value is in the array as dictionary value?

From Dev

Check value of array in array had same value

From Dev

Get full value from array using partial value

From Dev

getting value inside array with for loop

Related Related

  1. 1

    Fast way to check if partial value is present in large array of hashes

  2. 2

    $.inArray() : check if array value has "+" inside

  3. 3

    $.inArray() : check if array value has "+" inside

  4. 4

    how to check if a value inside an array is larger than a certain value

  5. 5

    Check each value inside a nested array of a dictionary Swift

  6. 6

    How to match letters and check that value is present inside array or not using PHP

  7. 7

    Check value inside Map

  8. 8

    pass a parameter to html partial and retrieve value inside the partial html

  9. 9

    Check / match string against partial strings in array

  10. 10

    Array inside Array echoing value not inside array

  11. 11

    Check if value exists in multidimensional array and edit other value inside same key

  12. 12

    How to check the value is present inside an array and insert the value into database using PHP

  13. 13

    check value inside a link javascript

  14. 14

    Searching for a value inside an array

  15. 15

    Check if string is in array inside dictionary

  16. 16

    How to check if inside an array PHP

  17. 17

    Check input value in array

  18. 18

    jQuery check if value not in array

  19. 19

    Check if an array has a value

  20. 20

    How to check for a value in an array

  21. 21

    Check if an array value is set or not

  22. 22

    Android find array index value of a partial match

  23. 23

    Android find array index value of a partial match

  24. 24

    Linq Check if array of string has a partial match in other array

  25. 25

    Linq Check if array of string has a partial match in other array

  26. 26

    Check if value is in the array as dictionary value?

  27. 27

    Check value of array in array had same value

  28. 28

    Get full value from array using partial value

  29. 29

    getting value inside array with for loop

HotTag

Archive