LINQ/Array operators mapping
| JS | LINQ |
|---|---|
concat |
Concat |
copyWithin |
? |
entries |
? |
every |
All |
fill |
Enumerable.Repeat(x, x).ToArray() |
filter |
? |
find |
? |
findIndex |
FindIndex |
findLast |
Last |
findLastIndex |
? |
flat |
? |
flatMap |
? |
forEach |
foreach |
includes |
Contains |
indexOf |
? |
join |
string.Join |
keys |
? |
lastIndexOf |
? |
map |
? |
pop |
? |
push |
? |
reduce |
? |
reduceRight |
? |
reverse |
Reverse |
shift |
? |
slice |
? |
some |
Any |
sort |
? |
splice |
? |
values |
? |
with |
? |
Get all selected options
Use the spread operator to convert a NodeList to an array so that the map function can be used
let selectedItems = [...document.querySelectorAll('[name="entries"]:checked')].map(x => x.value);
Convert to a CSV:
const selector = <HTMLSelectELement>document.querySelector('#mySelector');
const x = [...selector.selectedOptions].map(x => x.value).join(',');
Get unique values from table column
The Set object lets you store unique values of any type, whether primitive values or object references. Similar to HashSet in C#.
Following gets the text content of table cells and de-dupes them.
const orderItemTypes = document.querySelectorAll('td.orderitemtype') as NodeListOf<HTMLTableCellElement>;
const values = [...new Set([...orderItemTypes].map(x => x.textContent))];
Sum the total of values in an array using reduce
const totalSpace = [...ledger.querySelectorAll('tr:not(.d-none) td.size')]
.map(x => parseFloat(x.textContent))
.reduce((accumulator, currentValue) => accumulator + currentValue, 0);