You're using an Array like an "associative array", which does not exist in JavaScript. Use an Object ({}) instead.
If you are going to continue with an array, realize that toString() will join all the numbered properties together separated by a comma. (the same as .join(",")).
Properties like a and b will not come up using this method because they are not in the numeric indexes. (ie. the "body" of the array)
In JavaScript, Array inherits from Object, so you can add and delete properties on it like any other object. So for an array, the numbered properties (they're technically just strings under the hood) are what counts in methods like .toString(), .join(), etc. Your other properties are still there and very much accessible. :)
Read Mozilla's documentation for more information about Arrays.
var aa = [];
// these are now properties of the object, but not part of the "array body"
aa.a = "A";
aa.b = "B";
// these are part of the array's body/contents
aa[0] = "foo";
aa[1] = "bar";
aa.toString(); // most browsers will say "foo,bar" -- the same as .join(",")
Answer from Dominic Barnes on Stack OverflowYou're using an Array like an "associative array", which does not exist in JavaScript. Use an Object ({}) instead.
If you are going to continue with an array, realize that toString() will join all the numbered properties together separated by a comma. (the same as .join(",")).
Properties like a and b will not come up using this method because they are not in the numeric indexes. (ie. the "body" of the array)
In JavaScript, Array inherits from Object, so you can add and delete properties on it like any other object. So for an array, the numbered properties (they're technically just strings under the hood) are what counts in methods like .toString(), .join(), etc. Your other properties are still there and very much accessible. :)
Read Mozilla's documentation for more information about Arrays.
var aa = [];
// these are now properties of the object, but not part of the "array body"
aa.a = "A";
aa.b = "B";
// these are part of the array's body/contents
aa[0] = "foo";
aa[1] = "bar";
aa.toString(); // most browsers will say "foo,bar" -- the same as .join(",")
toString is a method, so you should add parenthesis () to make the function call.
> a = [1,2,3]
[ 1, 2, 3 ]
> a.toString()
'1,2,3'
Besides, if you want to use strings as keys, then you should consider using a Object instead of Array, and use JSON.stringify to return a string.
> var aa = {}
> aa['a'] = 'aaa'
> JSON.stringify(aa)
'{"a":"aaa","b":"bbb"}'
var result = String.fromCharCode.apply(null, arrayOfValues);
JSFiddle
Explanations:
String.fromCharCode can take a list of char codes as argument, each char code as a separate argument (for example: String.fromCharCode(97,98,99)).
apply allows to call a function with a custom this, and arguments provided as an array (in contrary to call which take arguments as is). So, as we don't care what this is, we set it to null (but anything could work).
In conclusion, String.fromCharCode.apply(null, [97,98,99]) is equivalent to String.fromCharCode(97,98,99) and returns 'abc', which is what we expect.
It depends on what you want and what you mean.
Option One: If you want to convert the text to ASCII, do this:
var theArray = [97,112,112,46,106,115,10,110,111,100,101,46,106,115,10];
theString = String.fromCharCode.apply(0, theArray);
(Edited based on helpful comments.)
Produces:
app.js
node.js
Option Two: If you just want a list separated by commas, you can do .join(','):
var theArray = [97,112,112,46,106,115,10,110,111,100,101,46,106,115,10];
var theString = theArray.join(',');
You can put whatever you want as a separator in .join(), like a comma and a space, hyphens, or even words.
Assuming the "output" is the content of this string you want, you can map the values first to wrap them in single-quotes, then join the array with commas and wrap the entire thing in "[...]".
const a = ['20','30','50'];
const str = `"[${a.map(v=>`'${v}'`).join(",")}]"`;
console.log(str);
Try this:
let arr = ['1','2','3'];
//this will convert it to string
let arrToString = JSON.stringify(arr);
//you can confirm its type using below code
console.log(typeof(arrToString))
You must use the join function on the array:
Copyvar teststring = array.join(",");
Copyarray.join();
That is the correct answer. If no value is supplied to the join method a comma is the default element separator. Use the following if you don't want any separator at all:
Copyarray.join("");
userList should be an object
userList = {};
userList[result.user_name] = result.user_password;
Alternatively, you can use Array.reduce
let results = [{user_name: "admin", user_password : "adminpass"}, {user_name: "user", user_password : "userpass"}]
let userList = results.reduce((o, {user_name, user_password}) => Object.assign(o, {[user_name] : user_password}), {});
console.log("user list: "+JSON.stringify(userList));
Using userList as an object is the correct way. But if you already have an array and must necessarily convert it into an object, taking elements 2 by 2, you may use this function:
function toObject(arr) {
var obj = {};
for (var i = 0; i < arr.length; i+=2)
obj[arr[i]] = arr[i+1];
return obj;
}
Demo
let result = ["admin","adminpass","user","userpass"]
function toObject(arr) {
var obj = {};
for (var i = 0; i < arr.length; i+=2)
obj[arr[i]] = arr[i+1];
return obj;
}
console.log(toObject(result));
How about this:
var obj = JSON.parse("{\"items\":" + request.param('items') + "}");
obj.title = request.param('title');
obj.thumb = request.param('thumb');
JSON.stringify(obj);
Perhaps I'm missing something, but this works just fine:
> a = '[{"name":"this"},{"name":"that"}]';
'[{"name":"this"},{"name":"that"}]'
> JSON.parse(a)
[ { name: 'this' }, { name: 'that' } ]
[email protected]