Monday, May 28, 2018

JavaScript Array Methods

Arrays in JavaScript are special kind of object, with numeric indexes starting at 0 and going up by 1 to how many items the array has, minus one (because of the zero-based index).

• Mutating / Non-mutating

• Returns Array / returns something else

LIFO (stack)

push(v) – adds an element v at the end of the array (at index length) and returns the new length of the array

pop() – removes the last element and returns the element that was deleted

FIFO (queue)

shift() – returns the first item of the list and deletes the item

push(v) – adds an element v at the end of the array (at index length) and returns the new length of the array

Reverse FIFO

unshift() – adds one or more elements to the beginning of an array and returns the new length of the array

pop() – removes the last element and returns the element that was deleted

Sorting

reverse() - reverses an array: [1,3,2].reverse() => [2,3,1]

sort(f) - returns sorted array according to the function: [1,3,2].sort((a,b) => a-b) => [1,2,3]; [1,3,2].sort((a,b) => b-a) => [3,2,1]

Manipulation

concat() - returns merged arrays: [1,2].concat([3,4]) => [1,2,3,4]

slice()

splice()

Iteration

every(f) – returns true if function returns true on every item: [1,2,3].every(x => x === 2) => false

fill(val,start,end) - returns an array with replaced values between indexes: [1,2,3,4,5].fill(9,1,3) => [1,9,9,4,5]

filter(f) – returns an array of all items for which the function returns true: [1,2,3].filter(x => x < 3) => [1,2]

find(f) - returns item identified (or undefined) in the function: [{a:1},{a:2},{b:3}].find(x => x.a === 2) => {a:2}

findIndex(f) - returns index (or -1) of an item identified in the function: `[{a:1},{a:2},{b:3}].findIndex(x => x.a === 2) => 1

flat() - returns an array of all items even in nested arrays (single level only) as a flat array: 1,[2,3].flat() => [1,[2],3]

flatMap(f) - returns flatten map (like map+flat) with a bug? in flat: [[1,2],3,[4]].flatMap(x => x * 2) => [NaN,9,16]

forEach(f) – no return value, runs the function on every element in the array: [1,2,3].forEach(x => ...).

includes(val) - returns whether the item exists in the array: [1,2,3].includes(4) => false

map(f) – returns a new list with the result of each item in the array: [1,2,3].map(x => x * 2) => [1,4,9]

some(f) – returns true if the function returns true for at least one of the items: [1,2,3].some(x => x === 2) => true

Reduction

reduce(v, a) - returns single value, computed by function from all of the items (SUMIF, kinda): [1,2,3].reduce((total, val) => total * val) => 6

reduceRight()

Thursday, May 17, 2018

QB to elems

Even I still adore Data Particles I “invented” almost 10 years ago, without proper tools (I struggle to create) it's quite hard to manage the data. I encounter it from time to time when I need to fix some order in our intranet app.

So for QB I decided to move from Data Particles to JSON structure I created for Qedy. It suits the purpose rather well and I can improve it to be mutually beneficial.

I created a simple parser, that runs for every ID, which is time consuming, but there's no rush. Data Particles will remain in use for caching. This way all the hardships disappear, because it won't be primary data. When an error occurs, it would be possible to simply delete all the invalid data and create it from the JSONs.