1

I have an array incident which is of the following format :-

var incident = 
[ [1, ['a', 'b', 'c'] ],
  [2, ['d', 'e'] ],
  [3, ['f', 'g', 'h'] ],
  [4, ['i'] ]
];

Now i want to extract data out of this array and format it something like this.

data = [ ["a", "b", "c"], ["d", "e"], ["f", "g", "h"], "i" ];

I tried formatting but no success. If anyone could help me out in this. Thanks a lot in advance.

1 Answer 1

4

You could use Array#map and check if the element has a length of one for a single element, otherwise return the array.

var incident = [[1, ['a', 'b', 'c']], [2, ['d', 'e']], [3, ['f', 'g', 'h']], [4, ['i']]],
    data = incident.map(function (a) {
        return a[1].length === 1 ? a[1][0] : a[1];
    });

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.