{"id":694,"date":"2023-11-08T19:46:00","date_gmt":"2023-11-09T01:46:00","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=694"},"modified":"2023-11-08T19:46:33","modified_gmt":"2023-11-09T01:46:33","slug":"array-of-integers-using-the-array-from-method","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/array-of-integers-using-the-array-from-method\/","title":{"rendered":"Array of integers using the Array.from() method"},"content":{"rendered":"\n<p>This code is using the <code>Array.from()<\/code> method to generate an array of numbers from 1 to 5. <\/p>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#FB8CFF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-javascript\">\nArray.from({ length: 5 }, (_, x) => x + 1)\n\/\/ > Array [1, 2, 3, 4, 5]\n\nArray.from({length: 10}, (_, x) => x + x );\n\/\/ > Array [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n<\/code><\/pre><\/div><\/div>\n\n\n\n<ul>\n<li><code>Array.from()<\/code> is a built-in JavaScript method that creates a new array from an iterable or array-like object. It takes an iterable object as its first argument and an optional mapping function as its second argument.<\/li>\n\n\n\n<li><code>{ length: 5 }<\/code> is an object literal with a <code>length<\/code> property set to 5. In this context, it is acting as an iterable object. This object represents a sequence of 5 items with <code>undefined<\/code> values.<\/li>\n\n\n\n<li><code>(_, x) =&gt; x + 1<\/code> is a mapping function. The first parameter <code>_<\/code> is a placeholder for the current element, and <code>x<\/code> represents the current index. The function increments the index <code>x<\/code> by 1, effectively generating an array of numbers starting from 1.<\/li>\n<\/ul>\n\n\n\n<p>When <code>Array.from()<\/code> is called with the iterable <code>{ length: 5 }<\/code> and the mapping function, it creates an array of numbers from 1 to 5. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This code is using the Array.from() method to generate an array of numbers from 1 to 5. When Array.from() is called with the iterable { length: 5 } and the mapping function, it creates an array of numbers from 1 to 5.<\/p>\n","protected":false},"author":1,"featured_media":258,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[36,1,35,49],"tags":[37,63],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/694"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=694"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/694\/revisions"}],"predecessor-version":[{"id":695,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/694\/revisions\/695"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/258"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}