1

I’ve got an JavaScript Array of Objects. These Objects have a unique identifier as a String. Since there is no need to have them in an array, how do I get rid of the outer array?

When trying to pop, (un)shift, etc. I’m always losing either UniqueStringA or UniqueStringB

I added a jsbin link here: https://jsbin.com/sulozeyelo/edit?js,console

What I got

const arrObj = [
    {
        "UniqueStringA": {
        "abc": [
            {
            "PropPair": {
                "a": "c",
                "b": "d"
            }
            },
            {
            "PropPair": {
                "a": "c",
                "b": "d"
            }
            }
        ],
        "def": [],
        "efg": [
            {
            "PropPair": {
                "a": "c",
                "b": "d"
            }
            },
            {
            "PropPair": {
                "a": "c",
                "b": "d"
            }
            }
        ],
        "xyz": []
        }
    },
    {
        "UniqueStringB": {
        "abc": [
            {
            "PropPair": {
                "a": "c",
                "b": "d"
            }
            },
            {
            "PropPair": {
                "a": "c",
                "b": "d"
            }
            }
        ],
        "def": [],
        "efg": [
            {
            "PropPair": {
                "a": "c",
                "b": "d"
            }
            },
            {
            "PropPair": {
                "a": "c",
                "b": "d"
            }
            }
        ],
        "xyz": []
        }
    }
];

Result should be like:

const obj =     {
    "UniqueStringA": {
        "abc": [
        {
            "PropPair": {
            "a": "c",
            "b": "d"
            }
        },
        {
            "PropPair": {
            "a": "c",
            "b": "d"
            }
        }
        ],
        "def": [],
        "efg": [
        {
            "PropPair": {
            "a": "c",
            "b": "d"
            }
        },
        {
            "PropPair": {
            "a": "c",
            "b": "d"
            }
        }
        ],
        "xyz": []
    },
    "UniqueStringB": {
        "abc": [
        {
            "PropPair": {
            "a": "c",
            "b": "d"
            }
        },
        {
            "PropPair": {
            "a": "c",
            "b": "d"
            }
        }
        ],
        "def": [],
        "efg": [
        {
            "PropPair": {
            "a": "c",
            "b": "d"
            }
        },
        {
            "PropPair": {
            "a": "c",
            "b": "d"
            }
        }
        ],
        "xyz": []
    }
};
3
  • 2
    let obj = arr[0] ... ??? Commented Dec 6, 2018 at 17:23
  • 1
    You can access through index or pop out the object using array#pop. Commented Dec 6, 2018 at 17:23
  • I've upvoted just for how this question is clear. Commented Dec 6, 2018 at 17:28

1 Answer 1

4

You can use Object.assign() and spread operator.

const arrObj = [
  {
    "uniqueStringA": {
      "abc": [{
        "propPair": {
          "A": "Hello",
          "B": "World"
        }
      }]
    }
  },
  {
    "uniqueStringB": {
      "abc": [{
        "propPair": {
          "A": "Hello",
          "B": "World"
        }
      }]
    }
  }];

const obj = Object.assign({}, ...arrObj );

console.log(obj);

Sign up to request clarification or add additional context in comments.

1 Comment

I updated my code. I’m losing uniqueStringA when doing you method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.