How to compare multiple arrays

Okay so I have data files with yaml like this:

colors:
          - name: Purple
            sizes:
              - Small
              - Medium
              - Large
              - X-Large
              - 2X-Large
              - 3X-Large
              - 4X-Large
          - name: Pink
            sizes:
              - Small
              - Medium
              - Large
              - X-Large
              - 2X-Large
              - 3X-Large
              - 4X-Large
          - name: Blue
            sizes:
              - Small
              - Medium
              - Large
              - X-Large
              - 2X-Large
              - 3X-Large
              - 4X-Large
              - 5X-Large

I basically want to compare the size arrays and return one list for the ones that match.

I can’t help but think that the best place to start is to check the length of the sizes arrays, but {{ if eq $len $len }} returns an error: error calling index: can't index item of type int

Likewise, I tried to match one array against another using some variation of:

{{ intersect (index .sizes 0) (index .sizes 1) }}

and got these errors:
error calling intersect: can't iterate over string and can't evaluate field sizes in type interface {}

Any pointing in the right direction would be great!

I’m not clear on what you want. Given the following, what do you want to end up with? [2] or [1, 2, 3, 4]?

colors:
  - sizes: [1, 2]
  - sizes: [2, 3]
  - sizes: [2, 3, 4]

And can you show the context around the intersect usage? How are you accessing the data file?

I suspect you meant something else, because the above does not make any sense ({{ if true …)

In that scenario I would want to print each size individually, because they’re all different, but given:

colors:
- sizes: [1, 2]
- sizes: [1, 2]
- sizes: [1, 2, 3]

I would like to return sizes: [1, 2] and sizes: [1, 2, 3]

@bep I won’t lie I did do exactly that in one iteration while on a cutting and pasting binge, but what I meant was:

if eq (len (index sizes 0)) (len (index sizes 1))

which returns not a thing, because len, sizes 0 counts the first item in the list, rather than the whole list, which brings me to the context you were asking about @moorereason.

The data file itself has this, which is probably where I went wrong in the first place. I thought about moving this to content, but then didn’t because the products don’t have pages.

brand_id:
title:
# parts of the product
items:
- name: Shirt
  style: something
  style_opts:
  # the literal html label for the style option
  - label: Product color
    colors:
    - name: 
      id:
      images:
      - image_name:
        img_url
     sizes:
      - sm
      - md

I know I provided a partial of this already, but I felt like I needed this to explain accessing the data file, because it really doesn’t like trying to dot chain over maps, again - I’m probably doing this wrong…

So I have content files for the “brand” that link to the brand_id of the “product” in data/products, which I access with a range:

{{- range $index, $products := $.Site.Data.products -}}

{{ end }}

Then I range over the maps in the sequence to get to where I’m going so:

{{ range .items }}
 {{ range .style_opts }}
   {{ range .colors }}
     {{ do stuff with the stuff in the colors }}
   {{ end }}
 {{ end }}
{{ end }}

So this is where I’m having trouble with intersect because my thought process is to determine if two lists are the same and I figured intersect would be perfect for that. I’m probably overreaching, but it seems like it could work.

That particular code throws a runtime error. Is that because the values in colors are key value pairs?

interset returns the intersection of two sets.

A = [ 1, 2, 3 ]
B = [ 2, 3, 4 ]
A ∩ B = [ 2, 3 ]

If you just want to know if two sets are equal, you should be able to use eq. I was thinking of something like this:

{{ range .style_opts }}
  {{ range .colors }}
    {{ $.Scratch.Set "found", false }}
    {{ $sizes := .sizes }}
    {{ range $.Scratch.Get "sizeList" }}
      {{ if eq . $sizes }}
        {{ $.Scratch.Set "found" true }}
      {{ end }}
    {{ end }}

    {{ if not $.Scratch.Get "found" }}
      {{ $.Scratch.Add "sizeList" $sizes }}
    {{ end }}
  {{ end }}

  {{ range $.Scratch.Get "sizeList" }}
    {{/* do something with the duplicated list */}}
  {{ end }}
{{ end }}

But $.Scratch.Add "sizeList" $sizes will append the items of $sizes into a single slice. I don’t think we currently have a way to create a slice of slices on the fly.

@bep, any thoughts?

Maybe you could start by stating what the problem you are trying to solve by giving some examples.

Sorry. I’m probably complicating what is a small e-commerce site.
There are several products that have different colors available in different sizes.

So the example would be leggings that are available in green, blue and black.

The green and the black are both available in small, medium and large, but the blue are only available in small, large and x-large.

I think I’m having the ultimate brain fart around this though. Maybe I should just show a different size select based on the color selected just like I would the image. DUH!!!

Sometimes you just need to type these things out loud.

Thank you guys for taking a look at this!