JavaScript Map or flatmap that works like Array.prototype.join in Javascript flatmap works

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.

Answer:1

You're looking for Ramda's intersperse.

R.intersperse('n', ['ba', 'a', 'a']); //=> ['ba', 'n', 'a', 'n', 'a']

Though it's implemented imperatively.

Answer:2

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);
Answer:3

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),
  [],
)
Answer:4

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, "-"))
Answer:5

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 ]
  )
Answer:6

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 ...

  1. bootstrap load content
  2. bootstrap load content on scroll
  3. bootstrap load content on tab click
  4. bootstrap load content on click
  5. bootstrap load content in modal
  6. bootstrap load div
  7. bootstrap load tab content ajax
  8. bootstrap load more content
  9. bootstrap load dynamic content
  10. bootstrap load content into div
  11. load a bootstrap popover content with ajax

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 ...

  1. javascript result object
  2. javascriptresult function
  3. javascript result in mvc
  4. javascript result type
  5. javascript result nan
  6. javascript result keyword
  7. javascript result in html
  8. javascript result
  9. javascript result.push
  10. javascript result undefined
  11. javascript result of assignment
  12. javascript result in asp.net mvc
  13. javascript result of promise
  14. javascript result array
  15. javascript result is not defined
  16. javascript result c#
  17. javascript result disappears
  18. javascript result variable
  19. javascript result to json
  20. javascript result.length

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) { });

  1. what does mean in math
  2. what does mean mean
  3. what does mean in text
  4. what does mean in python
  5. what does mean in excel
  6. what does mean in java
  7. what does mean in measuring
  8. what does mean in c++
  9. what does mean in r
  10. what does mean in spanish
  11. what does mean on snapchat
  12. what does mean in javascript
  13. what does mean in sql
  14. what does mean in geometry
  15. what does mean in statistics
  16. what does mean in coding
  17. what does mean national emergency
  18. what does mean absolute deviation mean
  19. what does mean in matlab
  20. what does mean in linux

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" ...