JS Snippets: Array: Difference between revisions

From WikiMLT
Spas (talk | contribs)
m Стадий: 5 [Фаза:Утвърждаване, Статус:Авторизиран]; Категория:JavaScript
Spas (talk | contribs)
m Стадий: 6 [Фаза:Утвърждаване, Статус:Утвърден]; Категория:JavaScript
Line 35: Line 35:
{{devStage  
{{devStage  
  | Прндл  = JavaScript
  | Прндл  = JavaScript
  | Стадий = 5
  | Стадий = 6
  | Фаза  = Утвърждаване
  | Фаза  = Утвърждаване
  | Статус = Авторизиран
  | Статус = Утвърден
  | ИдтПт  = Spas
  | ИдтПт  = Spas
  | РзбПт  = Spas
  | РзбПт  = Spas

Revision as of 11:50, 18 February 2023

In­ter­sect two Ar­rays

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]

Ref­er­ence: How to find the in­ter­sec­tion of ar­rays in JavaScript by Re­act­go

Get an Ar­ray el­e­ments by an Ar­ray of in­dex­es

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']