Module:Util/tables/mapValues

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search

mapValues(tbl, iteratee)

Returns

  • Creates a table with the same keys as tbl and values generated by running each value of tbl thru iteratee.

Examples

#InputOutputResult
1
local templateArgs = {
  arg1 = "  foo ",
  arg2 = "bar\n",
}
return mapValues(templateArgs, mw.text.trim)
{
  arg2 = "bar",
  arg1 = "foo",
}
property iteratee shorthand
2
mapValues(
  {
    Zelda = { Triforce = "Wisdom" },
    Link = { Triforce = "Courage" },
  },
  "Triforce"
)
{
  Link = "Courage",
  Zelda = "Wisdom",
}

local iterateeResolver = require("Module:Util/tables/iteratee")

local function mapValues(tbl, iteratee)
	iteratee = iterateeResolver(iteratee)
	local result = {}
	for k, v in pairs(tbl) do
		result[k] = iteratee(v)
	end
	return result
end

return mapValues