I find the functionality of Array.prototype.join
very useful because it only applies the join value to the "inner" connections of the elements in the array. Like so:
['Hey', 'there'].join('-') // Hey-there
Where Array.protoype.map
produces a 'leftover' dash in this example:
['Hey', 'there'].map(value => value + '-') // Hey-there-
I've been looking for a succinct way to map arrays without converting them to a string, possibly to a new array, like so:
// Intended behaviour
['Hey', 'there'].mapJoin('-') // ['Hey', '-', 'there']
I'm NOT looking for an imperative solution to the problem as I could write that myself and put it in a global import somewhere. I'm looking for a native way (ES6 is fine) to express it elegantly so I can write it in all my projects.
You're looking for Ramda's intersperse.
R.intersperse('n', ['ba', 'a', 'a']); //=> ['ba', 'n', 'a', 'n', 'a']
Though it's implemented imperatively.
You could join with a wanted separator, and split by the addition comma (or any other value, if taken for join).
var array = ['Hey', 'there'],
separator = '-',
result = array.join(',' + separator + ',').split(',');
console.log(result);
It's not exactly pretty or elegant, but
['Hey', 'there'].reduce(
(acc, value, i, arr) => (acc.push(value), i < arr.length - 1 ? acc.push('-') : 0, acc),
[],
)
You can try this one
const mapJoin = (arr, joiner) => {
return arr.reduce( (curr, t) => curr.concat(t, joiner), []).slice(0, arr.length*2-1)
}
const data = ["Hey", "There"]
console.log(mapJoin(data, "-"))
A simple recursive encoding
const intersperse = (sep, [ x, ...rest ]) =>
// base case; return empty result
x === undefined
? []
// one remaining x, return singleton
: rest.length === 0
? [ x ]
// default case; return pair of x and sep and recur
: [ x, sep ] .concat (intersperse (sep, rest))
console.log
( intersperse ("~", []) // []
, intersperse ("~", [ 1 ]) // [ 1 ]
, intersperse ("~", [ 1, 2 ]) // [ 1, ~, 2 ]
, intersperse ("~", [ 1, 2, 3 ]) // [ 1, ~, 2, ~, 3 ]
, intersperse ("~", [ 1, 2, 3, 4 ]) // [ 1, ~, 2, ~, 3, ~, 4 ]
)
I am using Bootstrap tabs to load a widget from a server. What I need is to load the tab content as soon as the user click the tab instead of the initial load of the page. For ex below is the HTML for ...
I am using Bootstrap tabs to load a widget from a server. What I need is to load the tab content as soon as the user click the tab instead of the initial load of the page. For ex below is the HTML for ...
Why I am getting the result of -'54'+30 expresion as -24 however when I tried to remove the (minus)- from expression then It is just concatenation of string and gives 5430 as output.Can anyone explain ...
Why I am getting the result of -'54'+30 expresion as -24 however when I tried to remove the (minus)- from expression then It is just concatenation of string and gives 5430 as output.Can anyone explain ...
i would like to know about, what does mean by module wrapper function and what does it do to my code ? (function (exports, require, module, __filename, __dirname) { });
i would like to know about, what does mean by module wrapper function and what does it do to my code ? (function (exports, require, module, __filename, __dirname) { });
I have an external pdf that I'd like to show embedded in my page. The following works fine: <object frame-resize data="www.cbu.edu.zm/downloads/pdf-sample.pdf" type="application/pdf" width="200px" ...
I have an external pdf that I'd like to show embedded in my page. The following works fine: <object frame-resize data="www.cbu.edu.zm/downloads/pdf-sample.pdf" type="application/pdf" width="200px" ...