Discord
Login
Community
DARK THEME

Filter list if objects

It there any way to filter list by object attribute?

There is a 'contains' function to look for specific items and also a 'sortList' one to ... sort a list :)
Check the documentation for details.

Otherwise, would you have an example list and explain what you would like to filter? More specific information would help, it all depends on what you try to achieve.

Live Long and Tinker

local b1 = object end b1.y = 1

local b2 = object end b2.y = 1

local b22 =object end b22.y = 1

local b3 = object end b3.y = 2

local b4 =object end b4.y = 2

my_list = [b1,b2,b3,b4,b22]

// Here I want to get list of all objects from my_list // where y = 2

Is something like this what you are looking for?

objects = [] // will contain all of the objects where y = 2 after the below code
for obj in my_list
  if obj.y == 2 then // condition to add to list
    objects.push(obj) // add to object list
  end
end

We can do this but I was thinking more about My_list.filter(obj.y ==2)

Ahhh, now we are getting somewhere. At least now it's clear what you are looking for.
Possible? Yes. ... Do I know how? No. LOL, one for the experts here and not for a tinkerer like me.

One can extend the List prototype for sure, similar to this:

// filter object.index by value
List.filterObj = function(index,value)
  local objects = [] 
  for obj in this
    if obj[index] == value then 
      objects.push(obj) 
    end
  end 
  return objects
end 

  local b1 = object end b1.y = 1
  local b2 = object end b2.y = 1
  local b22 =object end b22.y = 1
  local b3 = object end b3.y = 2
  local b4 =object end b4.y = 2

  my_list = [b1,b2,b3,b4,b22]
  filtered = my_list.filterObj("y",2)

That works, but is not as flexible as you want. Where my knowledge ends is the function call the way you want it.
My_list.filter(obj.y ==2) won't work by what know.
'obj.y==2' would be evaluated by the interpreter and passed through as false/true result.

The function call would have to look like this (I think):

My_list.filter(function() ?.y==2 end), same as one would have to do for the sortList function.

But I have no clue how that has to be processed inside the extension itself.

EXPERTS ASSEMBLE!
Curious to see the solution myself :)

P.S. I re-posted this on the discord channel, response there is sometimes faster :)

Thanks for all this info. Function way looks like good alternative for what I want to do.

Also can you provide discord so I can join and get more insight.

List.filter doesn't seem to be documented, but it exists and seems to be what you need (and what @TinkerSmith was suggesting).

You can just call it and give it a function that returns true when your intended condition is met.

This code should work:

filtered_list = my_list.filter( function(o) return o.y == 2 end )

Works for values and objects.

The discord us:
https://discord.gg/BDMqjxd

It's listed on the About page. Worth joining

@Skaruts Thanks for "List.filter". This is exactly what I was looking for!

Yarko

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community