0

I want to declare variables inline ,and use it in same line. like:

(var arr = ['1', '2']).forEach(a => alert(arr))

But it give me error(Unexpected token 'var'). so is it possible in js?

EDIT: it's just an example. my real usecase is this:

var arr = [{id: 1, tags: [1, 2]}, {id: 2, tags: [5,6]}]
(var f = arr.find(item => item.id == 1)).tags.map(t => t + f.id)

I want to avoid separate declaration because i'm creating a chaining library so that every thing should be chained together in 1 line (in a clean way however)

yaya
  • 7,675
  • 1
  • 39
  • 38
  • 1
    this is not needed `var arr = ` – muasif80 Oct 01 '19 at 17:29
  • 9
    What do you want to do? This seems like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378) – VLAZ Oct 01 '19 at 17:29
  • 7
    No, it's not possible. (You could use an IIFE though). What do you need `arr` for here? You can just call `forEach` on a literal: `;['1', '2'].forEach(a => alert(a))` (I'd however recommend `for (const a of ['1', '2']) alert(a);`). – Bergi Oct 01 '19 at 17:29
  • It's just an example. i'm creating a library with chaining syntax, i want to declare variable in one section and use it later. – yaya Oct 01 '19 at 17:32
  • @VLAZ sorry for not including real usecase, i included it. – yaya Oct 01 '19 at 18:10
  • @yaya That "real usecase" makes no sense. `f` would be either `undefined` or an object, you cannot sum (concatenate?) it to a tag number. Also, you haven't mentioned *why* you would need to declare the variable inline - nothing prevents you from simply writing a separate declaration. – Bergi Oct 01 '19 at 18:14
  • @Bergi sorry, i meant `f.id`. i want to declare variables inline, if its not possible please submit an answer and i will accept it. – yaya Oct 01 '19 at 18:16
  • 1
    @Bergi i want to avoid separate declaration because i'm creating a chaining library so that every thing should be chained together in 1 line (in a clean way however) – yaya Oct 01 '19 at 18:17

4 Answers4

3

This is not possible, there is no syntax for this in JavaScript.

I want to avoid separate declaration because i'm creating a chaining library so that every thing should be chained together in 1 line (in a clean way however)

For that, use an IIFE:

(f => f.tags.map(t => t + f.id))(arr.find(item => item.id == 1)).forEach(a => alert(a));

If you want the order to not be inverted, have a function in your chaining library that accepts a callback - the user can then refer multiple times to the value in the callback, which is the closest you get to "inline declaration":

pipe(arr.find(item => item.id == 1), f => f.tags.map(t => t + f.id)).forEach(a => alert(a));

// or with destructuring:
pipe(arr.find(item => item.id == 1), ({id, tags}) => tags.map(t => t + id)).forEach(a => alert(a));

See also How to simulate let expressions in JavaScript? or How to use let declarations as expressions? for a bit of background.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks. Can you also add the note that it's not possible to declare inline variable in js?(the note you mentioned in comment) and plus, the first one doesnt return the modifed data, right? execute this: ```var arr = [{id: 1, tags: [1, 2]}, {id: 2, tags: [5,6]}]; var aa = (f => f.tags.map(t => t + f.id))(arr.find(item => item.id == 1)) alert(aa)``` – yaya Oct 01 '19 at 18:31
  • @yaya Apart from the missing semicolon, that works just fine. The shorthand arrow notation does return the expression result. – Bergi Oct 01 '19 at 18:42
2

It appears you want to have a reference to the array inside of the loop. You can use the argument passed in which is a reference to the original array.

['1', '2'].forEach((item, index, arr) => console.log(item, arr))
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • 2
    This will satisfy OP's example but I'm still not really clear what they are actually after. The question still seems like an XY problem. – VLAZ Oct 01 '19 at 17:42
0

I ended up with this:

Array.prototype.tap = function(callback){callback(this); return this;}
[1, 2].tap(arr => arr.forEach(a => alert(a + '_' +arr)))
yaya
  • 7,675
  • 1
  • 39
  • 38
-4

Use eval to create variable and use callback to show data.

function declare_var(vr,vl,s){
  eval('window.'+vr+'='+vl+';');
  eval('s(window.'+vr+');')
}

declare_var('i_am_var','[1,2,3,4,5]',(r)=>{r.forEach(a=>console.log(a))});