3

I have the following array

 [Array(1), Array(1), Array(0)]

Here is the formatted version

[{…}]
[{…}]
[]

How can I remove [] ?

2
  • 2
    Can you give more details on it., Is that mean, you want to remove empty arrays? Commented Dec 1, 2017 at 5:53
  • What about mainArray.pop() ? :-D Commented Dec 1, 2017 at 6:34

3 Answers 3

13

So you have an array of arrays & you want to remove the empty array?

You can filter out the values you don't want by using .filter

E.g

const arr = [ [], ['1', '2'], ['3', '4'] ].filter(v => v.length > 0);
Sign up to request clarification or add additional context in comments.

1 Comment

update your sample as const arr = [ [], ['1', '2'], ['3', '4'] ].filter(v => v.length > 0);
6

Try to use filter, this will iterate through the your array and will return array item which are having length greater than 0

result = obj.filter(x=> x.length > 0)

Comments

5

You can check the length of your internal array to determine whether you should keep the value when you filter the array.

const arr = [Array(1), Array(1), Array(0)]

console.log(
  arr.filter(x => x && x.length)
)
<script src="https://codepen.io/synthet1c/pen/KyQQmL.js"></script>

Comments

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.