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 OverflowIf 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
How to join fields of an array of objects in javascript/typescript - Stack Overflow
Combine objects in an array of objects in Typescript?
merge two object arrays with Angular 2 and TypeScript? - Stack Overflow
How can I merge two arrays of different objects but equal length?
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
Are you looking for something like this? You'll need to add types, but you can merge the features arrays from all entries in the data array using vanilla JS methods.
- Create the
outputobject and specify itstype. - Use
Array#filterto select the entries indatawith the matching type. - Use
Array#flatMapto select thefeaturesarray from each of the entries above and project their contents into a single array.
const data = [
{ type: 'FeatureCollection', features: [1, 2, 3] },
{ type: 'FeatureCollection', features: [4, 5, 6] }
];
const output = {
type: 'FeatureCollection',
features: data
.filter(obj => obj.type === 'FeatureCollection')
.flatMap(obj => obj.features)
};
console.dir(output);
Just use a simple Array.prototype.reduce function and the spread operator
const original = [
{ type: "banana", features: ["34", "wow", "hotdog"] },
{ type: "banana", features: ["dog", "cat", "recursion"] },
];
const compress = (original) => original.reduce((acc, cur) => {
acc.features = [...(acc.features ?? []), ...cur.features];
return acc;
}, { type: (original[0].type) });
console.log(compress(original));
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.