How to remove NULL objects from my array

I have an object that has an array of another object in it. I need to remove any null ones, as pictured here.

In this example, I need my original object the way it is, but just remove the 3rd array element ( highlighted ) which is null.

Does anyone know how to iterate through this object and remove any NULL ones from my Repair array?

Thanks for your help!

John S.Web DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Without further context, use array.filter(). E.g.

<!DOCTYPE html>
<html>
    <head>
        <script>
            (function ()
            {
                var array = [
                     { id: 1 },
                     { id: 2 },
                     null,
                     { id: 4 }
                ];

                console.log(JSON.stringify(array, null, 0));
                array = array.filter(
                    function (e) {
                        return e != undefined;
                    }
                );

                console.log(JSON.stringify(array, null, 0));
            })();
        </script>
    </head>
    <body>
    </body>
</html>

Open in new window


p.s. think about how really helpful posting images is, when you ask code related questions.. means that we cannot copy and paste from a concise and complete example..
0
John S.Web DeveloperAuthor Commented:
Thank you for your reply. Duly noted about images.

I am new at Javascript so please bare with me. Can the above be made into a function where I can pass my object into and be returned the clean object? The syntax you used above is a bit confusing. Again, new. How would I pass my object into a function with no arguments expected?  ( I am sure that is a complicated answer, but I'd like to just get a working version ). Thank you again for your very quick post.
0
ste5anSenior DeveloperCommented:
E.g.

<!DOCTYPE html>
<html>
    <head>
        <script>
            function filterArray(obj)
            {
                var result = obj;
                result.array = result.array.filter(
                    function (e) {
                        return e != undefined;
                    }
                );

                return result;
            }

            (function () {
                var obj = {
                    id: 1,
                    array: [
                        { id: 1 },
                        { id: 2 },
                        null,
                        { id: 4 }
                    ]
                };

                console.log(JSON.stringify(obj, null, 0));
                obj = filterArray(obj);
                console.log(JSON.stringify(obj, null, 0));
            })();
        </script>
    </head>
    <body>
    </body>
</html>

Open in new window


or more flexible:

<!DOCTYPE html>
<html>
    <head>
        <script>
            function filterPropertyArray(obj, prop)
            {
                var result = obj;
                result[prop] = result[prop].filter(
                    function (e) {
                        return e != undefined;
                    }
                );

                return result;
            }

            (function () {
                var obj = {
                    id: 1,
                    array: [
                        { id: 1 },
                        { id: 2 },
                        null,
                        { id: 4 }
                    ]
                };

                console.log(JSON.stringify(obj, null, 0));
                obj = filterPropertyArray(obj, 'array');
                console.log(JSON.stringify(obj, null, 0));
            })();
        </script>
    </head>
    <body>
    </body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
John S.Web DeveloperAuthor Commented:
Works perfect. Thank you for your assistance.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.