JS Snippets: Array: Difference between revisions
From WikiMLT
m Стадий: 6 [Фаза:Утвърждаване, Статус:Утвърден]; Категория:JavaScript |
mNo edit summary |
||
Line 31: | Line 31: | ||
<syntaxhighlight lang="shell-session"> | <syntaxhighlight lang="shell-session"> | ||
(3) ['a', 'b', 'd', 'e'] | (3) ['a', 'b', 'd', 'e'] | ||
</syntaxhighlight> | |||
== Remove the Duplicate Array Items == | |||
<syntaxhighlight lang="javascript" class="code-continue"> | |||
const eggScrambleRecipe = [ | |||
"🥓 bacon", "🍳 eggs", "🫑 green peppers", "🥓 bacon", | |||
"🥓 bacon", "🧀 cheese", "🌶️ hot sauce","🥦 broccoli", | |||
"🧀 cheese", "🥦 broccoli", "🌶️ hot sauce", "🍳 eggs" | |||
]; | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" class="code-continue"> | |||
// Loop over the array, | |||
// note newArr.includes(entry) is a nesting loop! | |||
// Performance: 2.121855 ms | |||
function removeDupesFromArray_1(arr = []) { | |||
const newArr = []; | |||
for (const entry of arr) { | |||
if (newArr.includes(entry)) continue; | |||
newArr.push(entry); | |||
} | |||
return newArr; | |||
} | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" class="code-continue"> | |||
// Similar to the above but using filter | |||
// Performance: 0.1757799 ms | |||
function removeDupesFromArray_2(arr = []) { | |||
const obj = {}; | |||
for (const entry of arr) { | |||
obj[entry] = true; | |||
} | |||
const newArr = []; | |||
for (const key in obj) { | |||
newArr.push(key); | |||
} | |||
return newArr; | |||
} | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" class="code-continue"> | |||
// Similar to the above but using Array.filter() | |||
// Performance: 0.1652799 ms | |||
function removeDupesFromArray_3(arr = []) { | |||
const obj = {}; | |||
return arr.filter((entry) => { | |||
if (obj[entry]) return false; | |||
obj[entry] = true; | |||
return true; | |||
}); | |||
} | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" class="code-continue"> | |||
// By using a Set of items :) | |||
// Performance: 0.15741600 ms | |||
function removeDupesFromArray_4(arr = []) { | |||
return [...new Set(arr)]; | |||
} | |||
</syntaxhighlight><noinclude> | </syntaxhighlight><noinclude> | ||
<div id='devStage'> | <div id='devStage'> |
Revision as of 13:29, 11 March 2023
Intersect two Arrays
const arr1 = [1, 2, 3, 4];
const arr2 = [1, 2, 5, 6];
const intersection = arr1.filter(value => arr2.includes(value));
console.log(intersection);
(2) [1, 2]
Reference: How to find the intersection of arrays in JavaScript by Reactgo
Get an Array elements by an Array of indexes
const arr1 = ['a', 'b', 'c', 'd', 'e', 'f'];
const arr2 = [1, 3, 4, 0];
const intersection = arr1.filter((value, index) => arr2.includes(index)); // arr2.includes(index) ? true : false
console.log(intersection);
(3) ['a', 'b', 'd', 'e']
Remove the Duplicate Array Items
const eggScrambleRecipe = [
"🥓 bacon", "🍳 eggs", "🫑 green peppers", "🥓 bacon",
"🥓 bacon", "🧀 cheese", "🌶️ hot sauce","🥦 broccoli",
"🧀 cheese", "🥦 broccoli", "🌶️ hot sauce", "🍳 eggs"
];
// Loop over the array,
// note newArr.includes(entry) is a nesting loop!
// Performance: 2.121855 ms
function removeDupesFromArray_1(arr = []) {
const newArr = [];
for (const entry of arr) {
if (newArr.includes(entry)) continue;
newArr.push(entry);
}
return newArr;
}
// Similar to the above but using filter
// Performance: 0.1757799 ms
function removeDupesFromArray_2(arr = []) {
const obj = {};
for (const entry of arr) {
obj[entry] = true;
}
const newArr = [];
for (const key in obj) {
newArr.push(key);
}
return newArr;
}
// Similar to the above but using Array.filter()
// Performance: 0.1652799 ms
function removeDupesFromArray_3(arr = []) {
const obj = {};
return arr.filter((entry) => {
if (obj[entry]) return false;
obj[entry] = true;
return true;
});
}
// By using a Set of items :)
// Performance: 0.15741600 ms
function removeDupesFromArray_4(arr = []) {
return [...new Set(arr)];
}