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 OverflowTypescript `Array.join` literal return type
How to join fields of an array of objects in javascript/typescript - Stack Overflow
Joining property values of objects in an array
Defining return type of function that returns an array of results of calling a factory function with different specific generics
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
Here is a version that works with both readonly and mutable tuples types and a separator.
type Join<
TElements,
TSeparator extends string,
> = TElements extends Readonly<[infer First, ...infer Rest]>
? Rest extends ReadonlyArray<string>
? First extends string
? `${First}${Rest extends [] ? '' : TSeparator}${Join<Rest, TSeparator>}`
: never
: never
: '';
type MutableTuple = ["a", "b", "c"];
type MutableTupleJoined = Join<MutableTuple, " | ">;
// ^? "a | b | c"
type ReadonlyTuple = readonly ["a", "b", "c"];
type ReadonlyTupleJoined = Join<ReadonlyTuple, " | ">;
// ^? "a | b | c"
A readonly type often comes from const assertions e.g.
const tuple = ["a", "b", "c"] as const;
type Joined = Join<typeof tuple, " | ">;
When inlining the tuple type, it is usually mutable e.g.
type Joined = Join<["a", "b", "c"], " | ">;
const arr = ['a', 'b'] as const;
type JoinStrings<T extends readonly string[]> = T extends readonly [infer F, ...infer R]
? `${F}${JoinStrings<R>}`
: '';
const joinArray = <T extends readonly string[]>(arr: T): JoinStrings<T> => arr.join('') as JoinStrings<T>;
const joined = joinArray(arr);
Before join you need map array
const array = [
{ a: '1', b: '2' },
{ a: '3', b: '4' },
];
const result = array.map(_ => _.a).join(', ');
console.log(result);
In order to perform this through For loop.
result = [];
const array = [{ a: '1', b: '2' }, { a: '3', b: '4' }];
array.forEach(elm => result.push(elm.a));
console.log(result.join(", "));
// 1, 3