Asabeneh
Asabeneh

Reputation: 27

How to remove multiple outer braces in JavaScript?

I tried to iterate the following array, an array with multiple outer square brackets as in string but I couldn't manage to find a solution. Could someone know how to remove the first outer square bracket?

//Original array
           var multipleBraceArray = "[
          [
            [-74.026675, 40.683935],
            [-74.026675, 40.877483],
            [-73.910408, 40.877483],
            [-73.910408, 40.683935]
          ]
        ]";

        //This is the output I am looking for:
              var singleBraceArray = [
              [-74.026675, 40.683935],
              [-74.026675, 40.877483],
              [-73.910408, 40.877483],
              [-73.910408, 40.683935]
            ];

Upvotes: 0

Views: 94

Answers (2)

Bricky
Bricky

Reputation: 2745

You have a couple errors:

  1. Javascript doesn't let you do strings over multiple lines without using a template literal (backtick) or the \ character to signal a line-end
  2. You're starting with the input as an Array in a String, not an Array.

Try using this instead:

var arr =JSON.parse("[[[-74.026675, 40.683935],[-74.026675, 40.877483],[-73.910408, 40.877483],[-73.910408, 40.683935]]]");

Upvotes: 0

Nick
Nick

Reputation: 16576

You're just looking for the first element of your multipleBraceArray

var multipleBraceArray = [[[-74.026675, 40.683935], [-74.026675, 40.877483], [-73.910408, 40.877483], [-73.910408, 40.683935]]];

var singleBraceArray = multipleBraceArray[0];

console.log(singleBraceArray);

Upvotes: 1

Related Questions