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.
asked Oct 5, 2016 at 6:29
Nikhil Tikoo
3752 gold badges9 silver badges32 bronze badges
1 Answer 1
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; }
answered Oct 5, 2016 at 6:33
Nina Scholz
388k26 gold badges367 silver badges417 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js