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 OverflowHow to join fields of an array of objects in javascript/typescript - Stack Overflow
merge two object arrays with Angular 2 and TypeScript? - Stack Overflow
javascript - Joining property values of objects in an array - Stack Overflow
javascript - Typescript `Array.join` literal return type - Stack Overflow
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);
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
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.
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);