Use map instead of forEach and drop the parenthesis and the curly braces in the lambda:
var userNames = objectArr.map(o => o.userName).join(', ');
Joining property values of objects in an array
Typescript join specific properties of an array as string - Stack Overflow
How to join fields of an array of objects in javascript/typescript - Stack Overflow
javascript - Perform .join on value in array of objects
Probably like this:
const result = item.map(it => ({
name: it.name,
syllabi: it.syllabus.map(s => s.name).join(', ')
}));
https://jsfiddle.net/ernmtcgj/
You could reduce the data to an array, then join that
let obj = {"item": [{"_id": "5a48e0c100a5863454c0af2a","name": "Maths","created_by": "5a43ee3231ad5a6b0850d961","__v": 0,"created_on": "2017-12-31T13:06:09.957Z","active": 1,"grade": [],"syllabus": [{"_id": "5a47a5faed12d92d0c2449f4","name": "CBSE","description": "CBSE Syllabus","created_by": "5a43ee3231ad5a6b0850d961","__v": 0,"created_on": "2017-12-30T14:43:06.305Z","banner": 1,"active": 1},{"_id": "5a47a615ed12d92d0c2449f5","name": "State Board","description": "State Board Syllabus","created_by": "5a43ee3231ad5a6b0850d961","__v": 0,"created_on": "2017-12-30T14:43:33.328Z","banner": 1,"active": 1}]}]};
let l = obj.item.reduce((a, {name, syllabus}) =>
[name].concat(syllabus.map(({name}) => name))
, []);
console.log(l);
console.log(`${l.shift()} - ${l.join(', ')}`);
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
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);
Since you want a single string out of this, and not an array, you will want to use Array.forEach to concatenate onto an existing string object, like so:
let outStr = '';
array.forEach((ele, idx) =>
outStr += `${ele.key}: ${ele.text}${idx < array.length ? '' : ', '}`
);
You could also use Array.map like the folks in the comments above suggested, but you will need to join the result to produce a single string at the end.
const array = [
{
key: "title",
text: " Example Text title",
},
{
key: "description",
text: "Example Text description",
},
{
key: "video",
text: "Example Text video",
},
];
let result = "";
array.forEach((object, index) => {
result += `${object.key}: ${object.text}`
if(index < array.length - 1) {
result += ', ';
}
});
console.log(result);
Run code snippetEdit code snippet Hide Results Copy to answer Expand