I have yet to see this specific solution, but if exists I would love to have a link. I pretty much have the following:
const obj = {"key":
[
{ "name": { "companyName": "Something" } },
{ "otherThing": { "coolThing": "coolThingName" } },
]}
Something in this format. I would like to literally just change it to:
const obj = {"key":
{
{ "name": { "companyName": "Something" } },
{ "otherThing": { "coolThing": "coolThingName" } },
}}
But I for some reason cannot for the life of me figure out how.
Here is the code I have so far:
const arr = [
{ 'value': 'val', 'another': 'yessir'},
{ 'value1': 'val1', 'another1': 'yessir1'},
{ 'value2': 'val2', 'another2': 'yessir2'},
{ 'value3': 'val3', 'another3': 'yessir3'},
];
const arrayToObject = (arry) => {
let newObj = {};
newObj = arry.reduce((acc, cur) => {
return { ...acc, ...cur };
}, {});
return newObj;
};
The issue with this is that it returns the entire segment into one large object, whereas I need to preserve the sub objects.
EDIT: For clarification, here is my motivation:
The elasticsearch query I currently have is like this
"query": {
"bool": {
"must": [
]}}
But the proper elasticsearch syntax for the "must" is:
"query": {
"bool": {
"must": {
}}}
I am trying to run this conversion on the fly in my code