If you want to map objects to something (in this case a property). I think Array.prototype.map is what you're looking for if you want to code functionally.
(fiddle)
If you want to support older browsers, that are not ES5 compliant you can shim it (there is a polyfill on the MDN page above). Another alternative would be to use underscorejs's pluck method:
var users = [
{name: "Joe", age: 22},
{name: "Kevin", age: 24},
{name: "Peter", age: 21}
];
var result = _.pluck(users,'name').join(",")
Answer from Benjamin Gruenbaum on Stack Overflowjavascript - Perform .join on value in array of objects - Stack Overflow
javascript - How to join/add strings in an array object - Stack Overflow
Convert array of key:value pair objects to string
Is there a way to convert an array into string like ["P" ,"A","N","D","A"] to PANDA ?
If you want to map objects to something (in this case a property). I think Array.prototype.map is what you're looking for if you want to code functionally.
(fiddle)
If you want to support older browsers, that are not ES5 compliant you can shim it (there is a polyfill on the MDN page above). Another alternative would be to use underscorejs's pluck method:
var users = [
{name: "Joe", age: 22},
{name: "Kevin", age: 24},
{name: "Peter", age: 21}
];
var result = _.pluck(users,'name').join(",")
Well you can always override the toString method of your objects:
var arr = [
{name: "Joe", age: 22, toString: function(){return this.name;}},
{name: "Kevin", age: 24, toString: function(){return this.name;}},
{name: "Peter", age: 21, toString: function(){return this.name;}}
];
var result = arr.join(", ");
console.log(result);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You can use .map and .join functionalities too. Here .map will take the values based upon the key-pointName and .join is used to return an array as a string. The elements will be separated by a specified separator (here "-").
let array = [
{pointID: 1, pointName: "One" },
{pointID: 2, pointName: "Two" },
{pointID: 3, pointName: "Three" }
];
var permittedValues = array.map(value => value.pointName).join("-") ;
You can use Array.prototype.reduce function to return an array which contains pointName for each object in the array and join all value of the return array with -.
let data = [
{pointID: 1, pointName: "One" },
{pointID: 2, pointName: "Two" },
{pointID: 3, pointName: "Three" }
]
let result = data.reduce((accumulator, current) => {
return accumulator.concat(current.pointName);
}, []).join('-');
console.log(result);
Run code snippetEdit code snippet Hide Results Copy to answer Expand