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 OverflowVideos
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);
The spread operator is kinda cool.
this.results = [ ...this.results, ...data.results];
The spread operator allows you to easily place an expanded version of an array into another array.
You can read about spread operator here.
I think that you should use rather the following:
data => {
this.results = this.results.concat(data.results);
this._next = data.next;
},
From the concat doc:
The concat() method returns a new array comprised of the array on which it is called joined with the array(s) and/or value(s) provided as arguments.
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
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(', ')}`);