Page 1 of 1

json result and lodash function _.at

Posted: Monday 29 April 2019 11:10
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?

Re: json result and lodash function _.at

Posted: Monday 29 April 2019 12:00
by papoo
just 2 for loop and it works

Re: json result and lodash function _.at

Posted: Monday 29 April 2019 14:20
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

Re: json result and lodash function _.at

Posted: Monday 29 April 2019 14:26
by papoo
thanks for this