0

I have 2 types of array like this: array one,

Array(productId, categoryId)

   (2, 423) 
   (6, 859)
   (3, 423)
   (5, 859)

and another array Array((productId1, productId2), count)

   ((2, 6), 1), ((2, 3), 1), ((6, 5), 1), ((6, 3), 1)

I would like to filter the second array based on the first array, firstly I want to check array 2 to see if productId1 and productId2 having the same category, if yes, will keep, otherwise will filter out this element. So the list above will be filtered to remain:

   ( ((2, 3), 1), ((6, 5), 1) )

Can anybody help me with this? Thank you very much.

1 Answer 1

2

If you don't mind working with the first array as a map, ie:

scala> val categ_info = cats = Array((2, 423), (6, 859), (3, 423), (5, 859)).toMap
categ_info: Map[Int, Int] = Map(2 -> 423, 6 -> 859, 3 -> 423, 5 -> 859)

then we have (setting up example data as simple Ints for convenience):

val data = Array(((2, 6), 1), ((2, 3), 1), ((6, 5), 1), ((6, 3), 1))

data.filter { case ((prod1_id, prod2_id), _) => 
    categ_info(prod1_id) == categ_info(prod2_id) 
}

producing:

res2: Array[((Int, Int), Int)] = Array(((2, 3), 1), ((6, 5), 1))

as requested.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Shadowlands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.