Skip to content
Advertisement

Order childs of array

I have this array with parents and their children:

const array = [
    {
        'title':'America',
        'type': 1
    },
    {
        'title':'Brasil',
        'type': 0
    },
    {
        'title':'Ecuador',
        'type': 0
    },
    {
        'title':'Bolivia',
        'type': 0
    },
    {
        'title':'Europe',
        'type': 1
    },
    {
        'title':'Spain',
        'type': 0
    },
    {
        'title':'Italy',
        'type': 0
    },
    {
        'title':'Germany',
        'type': 0
    }
]

The problem that I have is that I need to order the children of each father, the result should be:

array_order = [
    {
        'title':'America',
        'type': 1
    },
    {
        'title':'Bolivia',
        'type': 0
    },
    {
        'title':'Brasil',
        'type': 0
    },
    {
        'title':'Ecuador',
        'type': 0
    },
    {
        'title':'Europe',
        'type': 1
    },
    {
        'title':'Germany',
        'type': 0
    },
    {
        'title':'Italy',
        'type': 0
    },
    {
        'title':'Spain',
        'type': 0
    }
]

That is the only information I have, the father has type 1 and his children are next in the list types 0.

Any help please.

Thanks.

By the way, I have tried this but I’m stucked:

let expTempFull = [];
let expTempFirst = [];
expTemp.forEach((el)=>{
                    if (el.type == 1){
                        if (expTempFirst.length == 0){
                            expTempFull.push(el);
                        }else{
                            expTempFirst = expTempFirst.sort((a, b) => a.title.localeCompare(b.title));
                            expTempFirst.forEach((ex)=>{
                                expTempFull.push(ex);
                            });
                            expTempFirst = [];
                            expTempFull.push(el);
                        }
                    } else {
                        expTempFirst.push(el);
                        if (el.title === expTemp[expTemp.length - 1].title){
                            expTempFull.push(el);
                        }
                    }
                });

Advertisement

Answer

You could treat any same type as own group, sort them and take a flat array as result.

const
    array = [{ title: 'America', type: 1 }, { title: 'Brasil', type: 0 }, { title: 'Ecuador', type: 0 }, { title: 'Bolivia', type: 0 }, { title: 'Europe', type: 1 }, { title: 'Spain', type: 0 }, { title: 'Italy', type: 0 }, { title: 'Germany', type: 0 }],
    result = array
        .reduce((r, o, i) => {
            if (!i || r[r.length - 1][0].type !== o.type) r.push([]);
            r[r.length - 1].push(o);
            return r;
        }, [])
        .flatMap(array => array.sort((a, b) => a.title.localeCompare(b.title)));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement