Skip to content

Instantly share code, notes, and snippets.

@tisseurdetoile
Last active November 11, 2022 10:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tisseurdetoile/98c0b945c9ccb337c572040b07d6c57f to your computer and use it in GitHub Desktop.
Save tisseurdetoile/98c0b945c9ccb337c572040b07d6c57f to your computer and use it in GitHub Desktop.
Array transformation for javaScript
/**
* Arrays to parse.
*/
const array_one = ['one', 'two', 'three', 'four']
const array_two = ['three', 'four', 'five', 'six']
const big_one = ['one', 'two', 'three', 'four', 'three', 'four', 'five', 'six']
const array_number = [1, 10, 100, 9, 7, 5, 4, 55, 76, 45, 32, 92, 41]
const array_obj_number = [
{ val: 1 },
{ val: 10 },
{ val: 100 },
{ val: 9 },
{ val: 7 },
{ val: 5 },
{ val: 4 },
{ val: 55 },
{ val: 76 },
]
/**
* UNION : Get the union of the two arrays
*/
let union = array_one.filter((x) => array_two.indexOf(x) >= 0)
// union => Array [ "three", "four" ]
console.log('# UNION : Get the union of the two arrays')
console.log('array_one=%s union=%s', array_one, union)
/**
* DISTINCT : Get the element not common in array_one and array_two __NOTA__ : need the union variable
*/
let distinct = array_one
.concat(array_two)
.filter((x) => union.indexOf(x) === -1)
let distinctV2 = [...array_one, ...array_two]
.concat()
.filter(
(x) => array_one.filter((x) => array_two.indexOf(x) >= 0).indexOf(x) === -1
)
// distinct => Array [ "one", "two", "five", "six" ]
// distinctV2 => Array [ "one", "two", "five", "six" ]
console.log('# UNION : Get the union of the two arrays')
console.log(
'array_one=%s + array_two=%s distinct=%s',
array_one,
array_two,
distinct
)
console.log(
'array_one=%s + array_two=%s distinctV2=%s',
array_one,
array_two,
distinct
)
/**
* UNIQ: filter array getting only uniq value
*/
let uniq = big_one.filter((elem, pos, arr) => arr.indexOf(elem) === pos)
// uniq => Array [ "one", "two", "three", "four", "five", "six" ]
console.log('# UNIQ: filter array getting only uniq value')
console.log('big_one=%s uniq=%s', big_one, uniq)
/**
* Int stuff max/min/sum/avg
*/
let max = Math.max.apply(Math, array_number) // max = 100
let min = Math.min.apply(Math, array_number) // min = 1
let sum = array_number.reduce((u, a) => u + a, 0) // sum = 477
let avg = sum / array_number.length // need sum => avg = 36.69...
let stddev = Math.sqrt(
array_number
.map(function (v) {
let d = v - avg
let s = d * d
return s
})
.reduce(function (u, a) {
return u + a
}, 0) / array_number.length
)
// standard deviation => stddev = 33,7.. need avg
console.log('# Int stuff max/min/sum/avg')
console.log('For int array = %s', array_number)
console.log('max = %s', max)
console.log('min = %s', min)
console.log('sum = %s', sum)
console.log('avg = %s', avg)
console.log('stddev = %s', stddev)
/**
* Array of object stuff max/min/sum/avg
*/
let max_obj = Math.max.apply(
Math,
array_obj_number.map((o) => o.val)
) // max_obj = 100
let min_obj = Math.min.apply(
Math,
array_obj_number.map((o) => o.val)
) // min_obj = 1
let sum_obj = array_obj_number
.map(function (o) {
return o.val
})
.reduce((u, a) => u + a, 0) // sum = 267
let avg_obj = sum_obj / array_obj_number.length
let stddev_obj = Math.sqrt(
array_obj_number
.map((o) => o.val)
.map(function (v) {
let d = v - avg_obj
let s = d * d
return s
})
.reduce((u, a) => u + a, 0) / array_obj_number.length
)
// standard deviation => stddev_obj = 35.20.. need avg_obj
console.log('# Array of Object')
console.log('For Object array = %s', array_obj_number)
console.log('max_obj = %s', max_obj)
console.log('min_obj = %s', min_obj)
console.log('sum_obj = %s', sum_obj)
console.log('avg_obj = %s', avg_obj)
console.log('stddev_obj = %s', stddev_obj)
/**
* Basic Array destructuring
*/
const [first, second, ...rest] = array_one
console.log('first elem = %s, second elem = %s,', first, second)
/**
* More complex array destructuring
*/
const {
0: start,
[array_one.length - 1]: end,
[array_one.length % 2 ? null : Math.ceil(array_one.length / 2)]: middle2,
[Math.ceil(array_one.length / 2) - 1]: middle1,
} = array_one
console.log(
'start elem = %s, end elem = %s, midle elems [%s,%s]',
start,
end,
middle1,
middle2
)
/**
* you can launch it with node arrays_tips.js
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment