{"id":2209,"date":"2019-06-18T10:37:41","date_gmt":"2019-06-18T09:37:41","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=2209"},"modified":"2019-06-21T13:01:24","modified_gmt":"2019-06-21T12:01:24","slug":"c-convert-array-of-objects-to-flat-csv-json-array-table","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-convert-array-of-objects-to-flat-csv-json-array-table\/","title":{"rendered":"C# Convert array of objects to flat (CSV\/JSON array) table"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    public class DataHelper\n    {\n        public static List&lt;object[]> GetDataTable&lt;T>(IEnumerable&lt;T> list)\n        {\n            Type type = typeof(T);\n            var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);\n            var rows = new List&lt;object[]>();\n            var headerRow = new object[properties.Length];\n            for (int i = 0; i &lt; properties.Length; i++)\n            {\n                var name = properties[i].Name;\n                headerRow[i] = char.ToLowerInvariant(name[0]) + name.Substring(1);\n            }\n            rows.Add(headerRow);\n            foreach(var item in list) { \n                var row = new object[properties.Length];\n                for (int i = 0; i &lt; properties.Length; i++)\n                {\n                    row[i] = properties[i].GetValue(item);\n                }\n                rows.Add(row);\n            }  \n            return rows;\n        }\n    }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Parse the JSON datatable in typescript<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const fromDataTable = (data: any[]): any[] => {\n    const firstRow = data.shift();\n    return data.map((row) => firstRow.reduce((obj, key, i) => ((obj[key] = row[i]) || 1) &amp;&amp; obj, {}));\n};<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Parse the JSON datatable in typescript<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-2209","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=2209"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2209\/revisions"}],"predecessor-version":[{"id":2255,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2209\/revisions\/2255"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=2209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=2209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=2209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}