getting value of array/object as string in javascript
javascript array to string without commas
array to string javascript
javascript object get value by key
javascript array to string with spaces
javascript array to string with comma
javascript map
javascript print array to html
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
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
Object.values(), The toString() method returns a string with all the array values, separated by commas. Note: This method will not change the original array. Browser Support. The� An array can hold many values under a single name, and you can access the values by referring to an index number. Creating an Array Using an array literal is the easiest way to create a JavaScript Array.
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);
JavaScript Array toString() Method, On ES6 compatible JS interpreters you can use an arrow function for brevity: allow you to provide a dot-separated string or array in order to access sub- properties: In Lodash you can get values of a property in array by following method const { name } = hero is an object destructuring. The destructuring defines a variable name with the value of property name. When you get used to object destructuring, you will find that its syntax is a great way to extract the properties into variables. Choose the object destructuring when you’d like to create a variable having the property
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());
From an array of objects, extract value of a property as array, So you may ask: What is special about array object then? An array Array = String; var arr = new Array(1, 2, 3, 4); // "1". One other Whenever you add a new element, the value of length will be updated. Also, you length simply returns the biggest index of the array + 1 –; you can clear the array by setting the length to 0. Use Object.entries(obj) to get an array of key/value pairs from obj. Use array methods on that array, e.g. map. Use Object.fromEntries(array) on the resulting array to turn it back into an object. For example, we have an object with prices, and would like to double them:
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) , []);
JavaScript Arrays - tips, tricks and examples, Array-like objects — those with a .length property and a value on the .length - 1 index — must be <script src="https://code.jquery.com/jquery-3.5.0.js"></script>. Return value. An array containing the given object's own enumerable property values. Description. Object.values() returns an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by looping over the property values of the object manually. Polyfill
jQuery.map(), This chapter focuses on Arrays, Objects and Functions. There are a var string = 'text/javascript; encoding=utf-8'; if (types.some(function(value) { return string. The JavaScript method toString () converts an array to a string of (comma separated) array values.
5. Arrays, Objects, Functions and JSON, length (to get the length of a string) and Math.max (the maximum function) in past chapters. These are expressions that access a property of some value. In the first � getting value of array/object as string in javascript. 3181 views December 2019 javascript 4 . T. trighati December 2019
Data Structures: Objects and Arrays :: Eloquent JavaScript, How to convert JavaScript arrays to strings. Covers join method plus type conversion of primitive values and objects to strings. Some common solutions to display JavaScript objects are: Displaying the Object Properties by name; Displaying the Object Properties in a Loop; Displaying the Object using Object.values() Displaying the Object using JSON.stringify()
Comments
- 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?
- no, i actually need the same data in view as in controller, my data turn to long string in view when passing
- you are using node? what templating engine are you using?
- i am using edge template engine with adonis-js as node provider
- 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
- 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 - tried adding .flat() on both sides (controller, view) did not work