1

I am passing my array from node controller to edge template. edit That array is going to convert into long string at view/client side. how would I get that array same as in controller.

Template Engine: Edge

Server Side: Adonis-JS (Node)

Controller:

//an array of locations
let locations = [
          ['Bondi Beach', -33.890542, 151.274856, 4],
          ['Coogee Beach', -33.923036, 151.259052, 5],
          ['Cronulla Beach', -34.028249, 151.157507, 3],
          ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
          ['Maroubra Beach', -33.950198, 151.259302, 1]
        ];

return view.render('dashboard.index', { locations: locations}) //passing to view

View:

var locations = {{locations}}
//var locations = Object.values({{locations}})

//this how inspect element is allocating values

var locations = Bondi Beach,-33.890542,151.274856,4,Coogee Beach,-33.923036,151.259052,5,Cronulla Beach,-34.028249,151.157507,3,Manly Beach,-33.80010128657071,151.28747820854187,2,Maroubra Beach,-33.950198,151.259302,1

how would I get the values like array to pass in locations var

4
  • I'm confused. What do you want your returned location to look like? Your last yellow block is a giant string, is that what you want? Commented Dec 27, 2019 at 4:55
  • no, i actually need the same data in view as in controller, my data turn to long string in view when passing Commented Dec 27, 2019 at 4:56
  • you are using node? what templating engine are you using? Commented Dec 27, 2019 at 4:57
  • i am using edge template engine with adonis-js as node provider Commented Dec 27, 2019 at 5:01

4 Answers 4

2

Try JSON.stringify()

//an array of locations
let locations = [
          ['Bondi Beach', -33.890542, 151.274856, 4],
          ['Coogee Beach', -33.923036, 151.259052, 5],
          ['Cronulla Beach', -34.028249, 151.157507, 3],
          ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
          ['Maroubra Beach', -33.950198, 151.259302, 1]
        ];

return view.render('dashboard.index', { locations: JSON.stringify(locations)}) //passing to view

View:

var locations = {{{locations}}}

Everything inside {{ }} will escape HTML and in order to write raw HTML, you must wrap it inside {{{ }}}

https://edge.adonisjs.com/docs/syntax-guide#_interpolation

Sign up to request clarification or add additional context in comments.

2 Comments

this is somehow worked, but var locations = [["Bondi Beach",-33.8.... quotes converted like this, is there a way to skip this conversion
actullay @ is added when any adonis method or pre-defined variable is called -- didnot work with it
0

You can nest forEach functions to do it:

let locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

var output = [];

locations.forEach(x => x.forEach(y => output.push(y)));

console.log(output);

5 Comments

then pass that output to view ?
@trighati Sure, you can try return view.render('dashboard.index', { locations: output})
still getting the same as always :(
@trighati Passing a string or an array to view is still getting same result. So the problem can be in another place.
may be view.render() is doing something hidden
0

I know someone recommended a nested forEach, but it's cleaner to use Array.prototype.reduce imo

i.e.

const result = locations.reduce((flattened, location) => 
 flattened.concat(...location)
, []);

Comments

0

Use .flat() :-

let locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

console.log(locations.flat());

1 Comment

tried adding .flat() on both sides (controller, view) did not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.