

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>array &#8211; Max的程式語言筆記</title>
	<atom:link href="https://stackoverflow.max-everyday.com/tag/array/feed/" rel="self" type="application/rss+xml" />
	<link>https://stackoverflow.max-everyday.com</link>
	<description>我要當一個豬頭，快樂過每一天</description>
	<lastBuildDate>Wed, 13 Mar 2024 09:22:23 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://stackoverflow.max-everyday.com/wp-content/uploads/2017/02/max-stackoverflow-256.png</url>
	<title>array &#8211; Max的程式語言筆記</title>
	<link>https://stackoverflow.max-everyday.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>create an array of unique values in JavaScript</title>
		<link>https://stackoverflow.max-everyday.com/2024/03/create-an-array-of-unique-values-in-javascript/</link>
					<comments>https://stackoverflow.max-everyday.com/2024/03/create-an-array-of-unique-values-in-javascript/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 13 Mar 2024 09:21:27 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=5562</guid>

					<description><![CDATA[解法1: 或 解法2: 解法3:]]></description>
										<content:encoded><![CDATA[
<p>解法1:</p>



<pre class="wp-block-code"><code>let uniqueArray = &#91;...new Set(&#91;5,5,2,2,2,4,2])];</code></pre>



<p>或</p>



<pre class="wp-block-code"><code>let uniqueArray = &#91;...new Set(dupeArray)];</code></pre>



<p>解法2:</p>



<pre class="wp-block-code"><code>let dupeArray = &#91;3,2,3,3,5,2];
let uniqueArray = Array.from(new Set(dupeArray));</code></pre>



<p>解法3:</p>



<pre class="wp-block-code"><code>if(this.items.indexOf(item) === -1) {
    this.items.push(item);
    console.log(this.items);
}</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2024/03/create-an-array-of-unique-values-in-javascript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to easily truncate an array with JavaScript?</title>
		<link>https://stackoverflow.max-everyday.com/2023/05/how-to-easily-truncate-an-array-with-javascript/</link>
					<comments>https://stackoverflow.max-everyday.com/2023/05/how-to-easily-truncate-an-array-with-javascript/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Thu, 11 May 2023 08:48:53 +0000</pubDate>
				<category><![CDATA[javascript筆記]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[String]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=4814</guid>

					<description><![CDATA[我有一個類似 stack 的 array , 想...]]></description>
										<content:encoded><![CDATA[
<p>我有一個類似 stack 的  array , 想只保留 array 裡的前N筆. </p>



<p>使用的情境是有一個檔案瀏覽時的 nav, 當不是最後一個項目時, 要允許使用者pop 到目的的階層:</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="850" height="466" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2023/05/2023-05-11-16_46_53-Workflow-Editor.jpg" alt="" class="wp-image-4815" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2023/05/2023-05-11-16_46_53-Workflow-Editor.jpg?v=1683794886 850w, https://stackoverflow.max-everyday.com/wp-content/uploads/2023/05/2023-05-11-16_46_53-Workflow-Editor-600x329.jpg?v=1683794886 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2023/05/2023-05-11-16_46_53-Workflow-Editor-768x421.jpg?v=1683794886 768w" sizes="(max-width: 850px) 100vw, 850px" /></figure>



<p>解法:<br><a href="https://stackoverflow.com/questions/953071/how-to-easily-truncate-an-array-with-javascript">https://stackoverflow.com/questions/953071/how-to-easily-truncate-an-array-with-javascript</a></p>



<p>There is a&nbsp;<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice">slice</a>&nbsp;method</p>



<pre class="wp-block-code"><code>let arr = &#91;1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
arr = arr.slice(0, 4);
console.log(arr);</code></pre>



<p>使用範例: <br>PS: 這個使用起來, 有一點點像 python 的 array 裡的用法, 例如: 負數指從右邊數過來.</p>



<pre class="wp-block-code"><code>const animals = &#91;'ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));
// Expected output: Array &#91;"camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// Expected output: Array &#91;"camel", "duck"]

console.log(animals.slice(1, 5));
// Expected output: Array &#91;"bison", "camel", "duck", "elephant"]

console.log(animals.slice(-2));
// Expected output: Array &#91;"duck", "elephant"]

console.log(animals.slice(2, -1));
// Expected output: Array &#91;"camel", "duck"]

console.log(animals.slice());
// Expected output: Array &#91;"ant", "bison", "camel", "duck", "elephant"]</code></pre>



<p>跟 .slice() 很像的是多一個p 的 splice().<br><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice</a></p>



<p>使用範例:</p>



<pre class="wp-block-code"><code>Remove 0 (zero) elements before index 2, and insert "drum"
const myFish = &#91;"angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum");

// myFish is &#91;"angel", "clown", "drum", "mandarin", "sturgeon"]
// removed is &#91;], no elements removed
Copy to Clipboard
Remove 0 (zero) elements before index 2, and insert "drum" and "guitar"
const myFish = &#91;"angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum", "guitar");

// myFish is &#91;"angel", "clown", "drum", "guitar", "mandarin", "sturgeon"]
// removed is &#91;], no elements removed
Copy to Clipboard
Remove 1 element at index 3
const myFish = &#91;"angel", "clown", "drum", "mandarin", "sturgeon"];
const removed = myFish.splice(3, 1);

// myFish is &#91;"angel", "clown", "drum", "sturgeon"]
// removed is &#91;"mandarin"]
Copy to Clipboard
Remove 1 element at index 2, and insert "trumpet"
const myFish = &#91;"angel", "clown", "drum", "sturgeon"];
const removed = myFish.splice(2, 1, "trumpet");

// myFish is &#91;"angel", "clown", "trumpet", "sturgeon"]
// removed is &#91;"drum"]
Copy to Clipboard
Remove 2 elements from index 0, and insert "parrot", "anemone" and "blue"
const myFish = &#91;"angel", "clown", "trumpet", "sturgeon"];
const removed = myFish.splice(0, 2, "parrot", "anemone", "blue");

// myFish is &#91;"parrot", "anemone", "blue", "trumpet", "sturgeon"]
// removed is &#91;"angel", "clown"]
Copy to Clipboard
Remove 2 elements, starting from index 2
const myFish = &#91;"parrot", "anemone", "blue", "trumpet", "sturgeon"];
const removed = myFish.splice(2, 2);

// myFish is &#91;"parrot", "anemone", "sturgeon"]
// removed is &#91;"blue", "trumpet"]
Copy to Clipboard
Remove 1 element from index -2
const myFish = &#91;"angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(-2, 1);

// myFish is &#91;"angel", "clown", "sturgeon"]
// removed is &#91;"mandarin"]
Copy to Clipboard
Remove all elements, starting from index 2
const myFish = &#91;"angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2);

// myFish is &#91;"angel", "clown"]
// removed is &#91;"mandarin", "sturgeon"]</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2023/05/how-to-easily-truncate-an-array-with-javascript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
