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.
Answer from Amsakanna on Stack OverflowCombine objects in an array of objects in Typescript?
Help with handling JSON array of objects (merge two arrays)
Merge multiple objects inside the same array into one object
Is there a benefit to using lodash.merge (or other utilities of the same feature) versus merging via object destructuring?
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.
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));
I have a json array of Objects and need to pick a corresponding object in that array according to some calculations.
My json have a following structure: {"OD": number, "SCH" {"SCH40": number, "SCH80": number}}
I need to do some math with OD and SCH numbers, compare it with an outside number and find corresponding value for OD key.
I can get a new array (of numbers I compare the outside number with):
this.Pipeareas = this.Pipesizes.map (x => Math.pow((x.OD - 2 * x.SCH[this.selectedSCH])/2000,2) * Math.PI);
How to merge existing and new arrays?
I tried (this add extra existing array and new object to new array (not key:value pair))
this.Pipeareas = {...this.Pipesizes, Area: this.Pipesizes.map (x => Math.pow((x.OD - 2 * x.SCH[this.selectedSCH])/2000,2) * Math.PI) }
and (this adds new key "Area" to each object but the value is full array, not corresponding numbers from new array)
this.Pipesizes.map (x => x.Area = this.Pipesizes.map (x => Math.pow((x.OD - 2 * x.SCH[this.selectedSCH])/2000,2) * Math.PI))
The result should be like:
return this.Pipeareas.find(x => x >= this.OutsideNumber);
If your environment supports Object.assign, then you can do the same in a succinct way like this
const arrObj = [{a: 1, b: 2}, {c: 3, d: 4}, {e: 5, f: 6}];
console.log(arrObj.reduce(function(result, current) {
return Object.assign(result, current);
}, {}));
// If you prefer arrow functions, you can make it a one-liner ;-)
console.log(arrObj.reduce(((r, c) => Object.assign(r, c)), {}));
// Thanks Spen from the comments. You can use the spread operator with assign
console.log(Object.assign({}, ...arrObj));
ES5 solution:
You can use Array.prototype.reduce like this
var resultObject = arrObj.reduce(function(result, currentObject) {
for(var key in currentObject) {
if (currentObject.hasOwnProperty(key)) {
result[key] = currentObject[key];
}
}
return result;
}, {});
console.log(resultObject);
# { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }
This solution, simply gathers all the keys and their values in every object in the result, which is finally returned to us as the result.
This check
if (currentObject.hasOwnProperty(key)) {
is necessary to make sure that we are not including all the inherited enumerable properties in the result.
You could use reduce for an elegant solution:
arrObj.reduce(function(acc, x) {
for (var key in x) acc[key] = x[key];
return acc;
}, {});
See MDN docs for reduce for more information.