EDIT: just realized this solution doesnt work, but its my closest attempt so far
const arr1 = ['a','b', 'c'];
const arr2 = ['a.foo', 'b.bar', 'c.baz'];
let result = {};
const insertAggs = (agg, result) => {
while (result.aggs) {
result = result.aggs
}
result.aggs = agg;
}
for (let i = 0; i < arr1.length; i++) {
const aggName = arr1[i];
const field = arr2[i];
const agg = {
[aggName]: {
terms: {
field
}
}
};
result.aggs ? insertAggs(agg, result) : result.aggs = agg;
}
}