-1

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

Jarred Parr
  • 1,167
  • 3
  • 11
  • 24

3 Answers3

0
{
  { "name": { "companyName": "Something" } },
  { "otherThing": { "coolThing": "coolThingName" } },
}

is not valid javascript object. Objects are key: value pairs

Like

{
  1: { "name": { "companyName": "Something" } },
  2: { "otherThing": { "coolThing": "coolThingName" } },
}


const arr = [
  { 'value': 'val', 'another': 'yessir'},
  { 'value1': 'val1', 'another1': 'yessir1'},
  { 'value2': 'val2', 'another2': 'yessir2'},
  { 'value3': 'val3', 'another3': 'yessir3'},
];

const arrayToObject = (arry) => {
  let newObj = {};
  arry.forEach((item, index) => {
    newObj[index] = item;
  }, {});
  return newObj;
};
agenthunt
  • 8,450
  • 3
  • 30
  • 26
  • @JarredParr Again this is not possible ```{"key": { { "name": { "companyName": "Something" } }, { "otherThing": { "coolThing": "coolThingName" } }, }}``` but instead ```{"key": { 1: { "name": { "companyName": "Something" } }, 2: { "otherThing": { "coolThing": "coolThingName" } }, }}``` or ```{"key": { name": { "companyName": "Something" } , otherThing": { "coolThing": "coolThingName" }, }}``` – agenthunt May 11 '18 at 12:11
-1

This is doable:

{ 
  "name": { "companyName": "Something" } , 
  "otherThing": { "coolThing": "coolThingName" } 
}

const arr = [
  { "name": { "companyName": "Something" } },
  { "otherThing": { "coolThing": "coolThingName" } },
], obj = {};
arr.forEach(function(item) {
  const key = Object.keys(item)[0];
  obj[key]=item[key];
})
console.log(obj);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
-1

Objects are key-value pairs and you have values without keys

{
  { "name": { "companyName": "Something" } },
  { "otherThing": { "coolThing": "coolThingName" } },
}

see the error in this snippet:

const obj = {"key":
{
  { "name": { "companyName": "Something" } },
  { "otherThing": { "coolThing": "coolThingName" } },
}}

Either of the following are valid objects:

    const obj = {
       "name": { "companyName": "Something" },
       "otherThing": { "coolThing": "coolThingName" },
    }
    
    console.log(obj)

or

    const obj = {
      0: { "name": { "companyName": "Something" } },
      1: { "otherThing": { "coolThing": "coolThingName" } },
    }
    
    console.log(obj);

According to the elasticsearch docs, either of the following are valid:

{
    "query" : {
      "bool" : {
        "must" : [
          {
            "term" : {
                "user" : "foo"
            }
          },
          {
            "term" : {
                "user" : "bar"
            }
          }
        ]
      }
    }
}

from here: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html

and

{
    "query": {
        "bool" : {
            "must" : {
                "query_string" : {
                    "query" : "some query string here"
                }
            },
            "filter" : {
                "term" : { "user" : "kimchy" }
            }
        }
    }
}

from here: https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html

So, I think what you're trying to accomplish is this:

    const yourArray = [
      { "name": { "companyName": "Something" } },
      { "otherThing": { "coolThing": "coolThingName" } },
    ]

    const preserveKeys = yourArray.reduce(function(acc, cur){
      for (let x in cur) {
        acc[x] = cur[x]
      }
      return acc;
    }, {});

    console.log(preserveKeys);

Just be careful you're not overwriting your keys with the ones from other objects

Jonathan Rys
  • 1,782
  • 1
  • 13
  • 25