JS Snippets: Array: Difference between revisions
From WikiMLT
mNo edit summary |
|||
Line 35: | Line 35: | ||
== Remove the Duplicate Array Items == | == Remove the Duplicate Array Items == | ||
<syntaxhighlight lang="javascript" class="code-continue"> | <syntaxhighlight lang="javascript" class="code-continue"> | ||
const | const arr = [ | ||
"🥓 bacon", "🍳 eggs", "🫑 green peppers", "🥓 bacon", | "🥓 bacon", "🍳 eggs", "🫑 green peppers", "🥓 bacon", | ||
"🥓 bacon", "🧀 cheese", "🌶️ hot sauce","🥦 broccoli", | "🥓 bacon", "🧀 cheese", "🌶️ hot sauce","🥦 broccoli", | ||
Line 45: | Line 45: | ||
// note newArr.includes(entry) is a nesting loop! | // note newArr.includes(entry) is a nesting loop! | ||
// Performance: 2.121855 ms | // Performance: 2.121855 ms | ||
function removeDupesFromArray_1(arr | function removeDupesFromArray_1(arr) { | ||
const newArr = []; | const newArr = []; | ||
Line 59: | Line 59: | ||
// Similar to the above but using filter | // Similar to the above but using filter | ||
// Performance: 0.1757799 ms | // Performance: 0.1757799 ms | ||
function removeDupesFromArray_2(arr | function removeDupesFromArray_2(arr) { | ||
const obj = {}; | const obj = {}; | ||
Line 77: | Line 77: | ||
// Similar to the above but using Array.filter() | // Similar to the above but using Array.filter() | ||
// Performance: 0.1652799 ms | // Performance: 0.1652799 ms | ||
function removeDupesFromArray_3(arr | function removeDupesFromArray_3(arr) { | ||
const obj = {}; | const obj = {}; | ||
Line 91: | Line 91: | ||
// By using a Set of items :) | // By using a Set of items :) | ||
// Performance: 0.15741600 ms | // Performance: 0.15741600 ms | ||
function removeDupesFromArray_4(arr | function removeDupesFromArray_4(arr) { | ||
return [...new Set(arr)]; | return [...new Set(arr)]; | ||
} | |||
</syntaxhighlight> | |||
== Flat an Array == | |||
<syntaxhighlight lang="javascript" class="code-continue"> | |||
const arr = [ | |||
[ "💰", "🐟", "🐟" ], | |||
[ ["🐟", "💐", "💐"], ["💵", "🏆"], "🐟" ], | |||
"🏆", "💐", "💵" | |||
]; | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" class="code-continue"> | |||
// Using the Built-in Array.flat() | |||
// Performance: ~0.3 ms | |||
function flatten_builtIn(arr) { | |||
return arr.flat(5); | |||
} | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" class="code-continue"> | |||
// Slow recursive solution | |||
// Performance: ~2.5 ms | |||
function flatten_1(arr) { | |||
let outputArr = []; | |||
for (const entry of arr) { | |||
if (Array.isArray(entry)) outputArr = [...outputArr, ...entry]; | |||
else outputArr.push(entry); | |||
} | |||
for (let i = 0; i < outputArr.length; i++) { | |||
if (Array.isArray(outputArr[i])) { | |||
outputArr.splice(i, 1, ...flatten(outputArr[i])); | |||
} | |||
} | |||
return outputArr; | |||
} | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="javascript" class="code-continue"> | |||
// Little bit faster non recursive solution | |||
// Performance: ~1.0 ms | |||
function flatten_2(arr) { | |||
const newArr = []; | |||
arr.forEach((el) => { | |||
if (Array.isArray(el)) { | |||
el.forEach((el) => newArr.push(el)); | |||
} else { | |||
newArr.push(el); | |||
} | |||
}); | |||
return newArr; | |||
} | } | ||
</syntaxhighlight><noinclude> | </syntaxhighlight><noinclude> |
Revision as of 14:38, 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 arr = [
"🥓 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)];
}
Flat an Array
const arr = [
[ "💰", "🐟", "🐟" ],
[ ["🐟", "💐", "💐"], ["💵", "🏆"], "🐟" ],
"🏆", "💐", "💵"
];
// Using the Built-in Array.flat()
// Performance: ~0.3 ms
function flatten_builtIn(arr) {
return arr.flat(5);
}
// Slow recursive solution
// Performance: ~2.5 ms
function flatten_1(arr) {
let outputArr = [];
for (const entry of arr) {
if (Array.isArray(entry)) outputArr = [...outputArr, ...entry];
else outputArr.push(entry);
}
for (let i = 0; i < outputArr.length; i++) {
if (Array.isArray(outputArr[i])) {
outputArr.splice(i, 1, ...flatten(outputArr[i]));
}
}
return outputArr;
}
// Little bit faster non recursive solution
// Performance: ~1.0 ms
function flatten_2(arr) {
const newArr = [];
arr.forEach((el) => {
if (Array.isArray(el)) {
el.forEach((el) => newArr.push(el));
} else {
newArr.push(el);
}
});
return newArr;
}