Concatenate Multiple Array Of Objects Into One And Use Outside Of Function
Solution 1:
sites.forEach
should be sites.map
, since while they both iterate over the elements in the array, only Array.prototype.map
returns a new modified array. That's only if you want to use this modified array. And you would need to return foo from myFunction
with something like
console.log(foo);
return foo;
Then, use Array.prototype.reduce
to make it a huge array. Add below sites.foreach
(now sites.map
)
// makes [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
// become [1, 2, 3, 4, 5, 6, 7, 8, 9]
sites.reduce((finalarray, array) => finalarray.concat(array))
Which just adds all the objects together. finalarray
is just the final result, while array is the current element. The reduce method still iterates over all elements.
EDIT:
Or you could use sites.flat
(taken from @Addis) and assign it to itself or a new variable
// Assigns to itself
sites = sites.map(myFunction).flat()
// Assigns to a new variable
let formattedSites = sites.map(myFunction).flat()
Solution 2:
Use the flat()
method to create a new array with all sub-array elements concatenated:
console.log(result.flat())
or
console.log([...result].flat())
let data = [{"site_nm": "gs Universe", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "gs Gameplay", "date": "2019-10-01", "sigh": 4, "yo": 3, "wokay": 2}, {"site_nm": "gs Universe Trailers", "date": "2019-10-01", "sigh": 5, "yo": 5, "wokay": 0}, {"site_nm": "TR", "date": "2019-10-01", "sigh": 4, "yo": 4, "wokay": 0}, {"site_nm": "gs", "date": "2019-10-01", "sigh": 5, "yo": 5, "wokay": 2}, {"site_nm": "GB", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 1}, {"site_nm": "cn", "date": "2019-10-01", "sigh": 4, "yo": 4, "wokay": 1}, {"site_nm": "Roadshow", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "TV", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "ZD", "date": "2019-10-01", "sigh": 2, "yo": 2, "wokay": 0}, {"site_nm": "Carfection", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "gs Trailers", "date": "2019-10-01", "sigh": 3, "yo": 3, "wokay": 0}, {"site_nm": "gs News", "date": "2019-10-01", "sigh": 3, "yo": 3, "wokay": 0}, {"site_nm": "gs Mobile", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "gs Universe Trailers", "date": "2019-10-02", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "gs Gameplay", "date": "2019-10-04", "sigh": 6, "yo": 6, "wokay": 2}, {"site_nm": "Roadshow", "date": "2019-10-02", "sigh": 2, "yo": 2, "wokay": 0}, {"site_nm": "gs", "date": "2019-10-02", "sigh": 3, "yo": 3, "wokay": 2}, {"site_nm": "TR", "date": "2019-10-02", "sigh": 4, "yo": 3, "wokay": 0}, {"site_nm": "cn Highlights", "date": "2019-10-03", "sigh": 8, "yo": 8, "wokay": 0}, {"site_nm": "gs Universe", "date": "2019-10-02", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "cn en Espa\u00f1ol", "date": "2019-10-02", "sigh": 6, "yo": 6, "wokay": 0}, {"site_nm": "gs Trailers", "date": "2019-10-02", "sigh": 2, "yo": 2, "wokay": 0}, {"site_nm": "ZD", "date": "2019-10-02", "sigh": 4, "yo": 4, "wokay": 1}, {"site_nm": "cn", "date": "2019-10-05", "sigh": 5, "yo": 5, "wokay": 1}, {"site_nm": "TV", "date": "2019-10-02", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "CH", "date": "2019-10-02", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "GB", "date": "2019-10-02", "sigh": 3, "yo": 3, "wokay": 3}, {"site_nm": "DLNow", "date": "2019-10-02", "sigh": 1, "yo": 0, "wokay": 0}, {"site_nm": "gs News", "date": "2019-10-02", "sigh": 2, "yo": 2, "wokay": 0}]
let bigArray = [];
let sites = [...newSet(data.map(({
site_nm
}) => site_nm))].sort();
let dates = [...newSet(data.map(({
date
}) => date))].sort();
sites.forEach(myFunction);
functionmyFunction(item) {
var foo = data.filter(function(e) {
return e.site_nm == item;
});
let foo_dates = [...newSet(foo.map(({
date
}) => date))].sort();
for (
var date = moment(dates[0], "YYYY-MM-DD"); date <= moment(dates[dates.length - 1], "YYYY-MM-DD"); date = date.add(1, 'days')) {
var dateFormatted = date.format("YYYY-MM-DD");
if (!foo_dates.includes(dateFormatted)) {
foo.push({
"site_nm": item,
"date": dateFormatted,
"sigh": "",
"yo": "",
"wokay": ""
});
}
}
bigArray.push(foo);
}
console.log(bigArray.flat());
Here I have added another array (bigArray
) to hold all foo
s and then applied flat()
.
Post a Comment for "Concatenate Multiple Array Of Objects Into One And Use Outside Of Function"