0

I need to find a way to transform var input =["tag1", "tag2", "tag3"] so that it becomes "tag1", "tag2", "tag3"

At the end I should have var output = "tag1", "tag2", "tag3" enter code here

I tried push, splice, join...nothing worked.

Some words on why: It can seem weird but I need to use some third party tool querying convention

object.containsAll('activities', 'tag1', 'tag2', 'tag3')

So the query is failing because I can't write object.containsAll('activities', input), as input here is an array

Mathieu
  • 3,841
  • 9
  • 45
  • 89
  • 1
    I know...that's why i added the "some words on why".I need to have a query that takes the items of the array to make: object.containsAll('activities', 'tag1', 'tag2', 'tag3') – Mathieu Sep 04 '18 at 22:30

1 Answers1

3

Why not spread the input array into the argument list, no transformation needed?

object.containsAll('activities', ...input);

Or, without spread:

const argumentsArr = input.slice(); // avoid mutating the original array
argumentsArr.unshift('activities');
object.containsAll.apply(object, argumentsArr);
CertainPerformance
  • 260,466
  • 31
  • 181
  • 209
  • I don't know this ... => what's the name of this technique – Mathieu Sep 04 '18 at 22:31
  • @Mathieu `=>` is the lambda, but it is not really related to the question. Or perhaps you mean the `...` it is the spread operator. – Attersson Sep 04 '18 at 22:32
  • @Mathieu: See this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax – mwilson Sep 04 '18 at 22:32
  • wow never knew this thing. thanks i'll try this out and will read more about these techniques – Mathieu Sep 04 '18 at 22:34
  • Perhaps illustrate that the spread operator is Es6 and that for backwards compat you can use ‘object.containsAll.apply(null, [‘activities’].concat(input))’ – Geert-Jan Sep 04 '18 at 22:36
  • 1
    @Mathieu Just read [What does this symbol mean in JavaScript?](https://stackoverflow.com/q/9549780/4642212) and the documentation on MDN about [expressions and operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators). – Sebastian Simon Sep 04 '18 at 22:40