After reading other table methods, it seems that there is nothing to say about it. The comments have already been clearly written, so I won't be embarrassed here ~ (TOM: lazy ...). Reference: quick_cocos.
Xingyue's contribution ~~~
-- [[-- Return all values in the specified table -- Example local T = {A = 1, B = 2, c = 3} local values = table. values (t) -- values = {1, 2, 3} -- @ parame t the table to be checked (T indicates table) -- @ parame table returns all values of the specified table, it is a value set table --] function table. values (t) local values = {} For _, V in pairs (t) DO Values [# values + 1] = V end return valuesend -- [[-- Insert the source table to the specified position of the target table, if no location is specified, connect two tables -- Example local tdest = {1, 2, 3} Local TS Rc = {4, 5, 6} table. insertto (tdest, tsrc) -- tdest = {1, 2, 3, 4, 5, 6} tdest = {1, 2, 3} table. insertto (tdest, tsrc, 5) -- tdest = {1, 2, 3, nil, 4, 5, 6} -- @ Param tdest target table (T indicates table) -- @ Param tsrc source table (T indicates table) -- @ Param ibegin insert position (I indicates integer) --] function table. insertto (tdest, tsrc, ibegin) Begin = checkint (ibegin) If begin <= 0 then begin = # tdest + 1 end local Len = # tsrc For I = 0, len-1 do tdest [I + begin] = tsrc [I + 1] endend -- [[-- query the specified value in the table and return its index, if the query fails, false is returned -- Example local T = {"A", "B", "C"} table. indexof (T, "B") -- output 2 -- @ Param t table (T indicates table) -- @ Param value the value to be searched -- @ Param ibegin start index value (I indicates integer) -- @ return integer returns the corresponding index, for example, 5 --] function table. indexof (T, value, ibegin) for I = ibegin or 1, # t do if T [I] = value then return I end return false End -- [[-- query the specified value in the table and return its key. If the query fails, Nil -- Example local T = {name = "A", name2 = "B ", name3 = "C"} table. keyof (T, "B") -- output name3 -- @ Param t table (T indicates table) -- @ Param value the value to be searched -- @ return string returns the corresponding key, for example: "name" --] function table. keyof (T, value) for K, V in pairs (t) do if v = value then return K end return nilend -- [[-- delete a specified value from the table, returns the number of Deleted Values -- Example local T = {"A", "B", "C", "C"} tabl E. removebyvalue (T, "C", true) -- output 2 -- @ Param t table (T indicates table) -- @ Param value the value to be deleted -- @ Param bremoveall whether to delete all identical values (B Indicates booleam) -- @ return integer returns the number of Deleted Values --] function table. removebyvalue (T, value, bremoveall) Local C, I, max = 0, 1, # T while I <= max do if T [I] = value then table. remove (t, I) C = C + 1 I = I-1 max = max-1 if not bremoveall then break end I = I + 1 end return Cend -- [[-- Execute a specified function for each value in the table and update the table content with the function return value -- Example local T = {name = "dualface", comp = "chukong"} table. map (T, function (v, k) -- add brackets before and after each value return "(".. V .. ")" End) -- output the modified table content for K, V in pairs (t) Do print (K, v) end -- output -- name (dualface) the function specified by the -- comp (chukong) FN parameter has two parameters and returns a value. The prototype is as follows: function map_function (value, key) return value end -- @ Param T (t indicates table) -- @ Param FN function --] function table. map (T, FN) for K, V in pairs (t) do t [k] = FN (v, k) endend -- [-- execute a specified function for each value in the table, but do not update the table content -- Example local T = {name = "dualface ", comp = "chukong"} table. walk (T, function (v, k) -- outputs each value print (v) End) The function specified by the FN parameter has two parameters and returns a value. The prototype is as follows: function map_function (value, key) end -- @ Param t table (T indicates table) -- @ Param FN function --] function table. walk (T, FN) for K, V in pairs (t) Do FN (v, k) endend -- [[-- execute a specified function for each value in the table, if this function returns false, the corresponding value is deleted from the table -- Example local T = {name = "dualface", comp = "chukong"} table. filter (T, function (v, k) return V ~ = "Dualface" -- if the value is dualface, the end value is filtered out. -- output the filtered table content. The function specified by the comp chukong FN parameter has two parameters, and return a Boolean value. The prototype is as follows: function map_function (value, key) return true or false end -- @ Param t table (T indicates table) -- @ Param FN function --] function table. filter (T, FN) for K, V in pairs (t) do if not FN (v, k) Then T [k] = nil end endend -- [[-- traverse the table, make sure the value is unique -- Example local T = {"A", "A", "B", "C"} Local n = table. unique (t) -- N = {"A", "B", "C" }== duplicate a is filtered out -- @ Param t table (T indicates table) -- @ return table returns the table containing all unique values --] function table. unique (t) Local check = {} Local n = {} for K, V in pairs (t) do if not check [v] Then N [k] = V check [v] = true end return nend
The author uses cocos2d-x 3.0 + Lua learning and work experience, without the author's consent, please do not reprint! Thank you for your patience ~~~
This article is not approved by the author and cannot be reproduced. Otherwise, relevant responsibilities will be held accountable. For more information, see the source !!~~
Address: http://www.cnblogs.com/wodehao0808/p/4029500.html
(Original) cocos2d-x 3.0 + Lua learning and working (4): common functions (7): Other Table Methods