json result and lodash function _.at

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

json result and lodash function _.at

Post by papoo »

hello
i have a json result table1 like

Code: Select all

{
{["name"]="aaaa", ["level"]=0}, 
{["name"]="bbbb", ["level"]=3}, 
{["name"]="cccc", ["level"]=0}, 
{["name"]="dddd", ["level"]=1}, 
{["name"]="eeee", ["level"]=3}, 
{["name"]="ffff", ["level"]=0}
}
i want to compare it with an array table2 like

Code: Select all

table2 = {"bbbb", dddd","eeee"}
for result

Code: Select all

{
{["name"]="bbbb", ["level"]=3}, 
{["name"]="dddd", ["level"]=1}, 
{["name"]="eeee", ["level"]=3}
}
but

Code: Select all

 _.print(_.at(table1, table2))
don't works. how can i do that?

eventually it is possible to return only high same level?
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: json result and lodash function _.at

Post by papoo »

just 2 for loop and it works
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: json result and lodash function _.at

Post by waaren »

papoo wrote: Monday 29 April 2019 11:10
I have a json result table1 ; I want to compare it with an array table2 and store result in table3
Please find an approach to this. Might very well be there are better ways to work this but at least this works :)

Code: Select all

return {
    on = {
        devices = { 'XButton-1'}
        },

    execute = function(dz)
        local _ = require('lodash');
        local table1 = {
                            {["name"]="aaaa", ["level"]=0}, 
                            {["name"]="bbbb", ["level"]=3}, 
                            {["name"]="cccc", ["level"]=0}, 
                            {["name"]="dddd", ["level"]=1}, 
                            {["name"]="eeee", ["level"]=3}, 
                            {["name"]="ffff", ["level"]=0}
                            }

        local table2 =  { "bbbb", "dddd", "eeee" }
        local table3 = {}
        
        local function myLines(str)
            print("");print("--------------------------------");print(str);print("--------------------------------")
        end
        
        myLines("Using lodash")
        _.forEach(table2, function(key)       -- looping though table2 values
            print(key .. " ==>> " .. table1[_.findIndex(table1, function(record)
                return record.name == key
            end)].level)
        end)
        
        myLines("Using standard Lua")
        for _, key in ipairs(table2) do
            for idx, record in ipairs(table1) do
                if key == record.name then
                    print(key .. " ==>> " .. record.level)
                end
            end
        end
        
        -- print("Insert in table3 using lodash")
        _.forEach(table2, function(key)       -- looping though table2 values
            table.insert(table3,{name = key, level = table1[_.findIndex(table1, function(record)
                                        return record.name == key
                                    end)].level})
          end)
        myLines("Inserted in table3 and print using lodash")
        _.print(table3)
        
        myLines("Inserted in table3 and print using dzVents dumpTable")
        dz.utils.dumpTable(table3, "> ")
        
   end
}
output
Spoiler: show
--------------------------------
Using lodash
--------------------------------
bbbb ==>> 3
dddd ==>> 1
eeee ==>> 3

--------------------------------
Using standard Lua
--------------------------------
bbbb ==>> 3
dddd ==>> 1
eeee ==>> 3

--------------------------------
Inserted in table3 and print using lodash
--------------------------------
{{["name"]="bbbb", ["level"]=3}, {["name"]="dddd", ["level"]=1}, {["name"]="eeee", ["level"]=3}}

--------------------------------
Inserted in table3 and print using dzVents dumpTable
--------------------------------
> 1:
> name: bbbb
> level: 3
> 2:
> name: dddd
> level: 1
> 3:
> name: eeee
> level: 3
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: json result and lodash function _.at

Post by papoo »

thanks for this
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest