最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Remove outer Array of Objects in JavaScript - Stack Overflow

matteradmin4PV0评论

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: ,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": []
    }
};

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./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": []
    }
};
Share Improve this question edited Dec 6, 2018 at 18:10 Kalaschnik asked Dec 6, 2018 at 17:22 KalaschnikKalaschnik 9081 gold badge12 silver badges26 bronze badges 3
  • 2 let obj = arr[0] ... ??? – jonny Commented Dec 6, 2018 at 17:23
  • 1 You can access through index or pop out the object using array#pop. – Hassan Imam Commented Dec 6, 2018 at 17:23
  • I've upvoted just for how this question is clear. – Nurbol Alpysbayev Commented Dec 6, 2018 at 17:28
Add a ment  | 

1 Answer 1

Reset to default 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);

Post a comment

comment list (0)

  1. No comments so far