简体   繁体   中英

How to implement Javascript logic in creating array with parents only has children

I am not very good at implementing logic, hence seeking your help to implement the following logic.

I have an js object array in parent child format in the following way:

"categories": {
    "data": {
        "1": {
            "id": 1,
            "name": "Churches & Religious Org.",
            "description": "Church\/religious organization",
            "parent": 267,
            "icon": "home"
        },
        "2": {
            "id": 2,
            "name": "Bands & Musicians",
            "description": "Musician\/band",
            "parent": 259,
            "icon": "music-note"
        },
        "3": {
            "id": 3,
            "name": "Products & Services",
            "description": "Product\/service",
            "parent": 260,
            "icon": "cube"
        },
        "4": {
            "id": 4,
            "name": "Actors & Directors",
            "description": "Actor\/director",
            "parent": 256,
            "icon": "film-marker"
        },
        "5": {
            "id": 5,
            "name": "Athletes & Players",
            "description": "Athlete",
            "parent": 264,
            "icon": "ios-americanfootball"
        },
        "6": {
            "id": 6,
            "name": "Movies",
            "description": "Movie",
            "parent": 256,
            "icon": "ios-film"
        },
        "7": {
            "id": 7,
            "name": "TV Shows",
            "description": "TV show",
            "parent": 256,
            "icon": "ios-videocam"
        },
        "8": {
            "id": 8,
            "name": "Professional Teams",
            "description": "Sports Team",
            "parent": 264,
            "icon": ""
        },
        "9": {
            "id": 9,
            "name": "Politicians",
            "description": "Politician",
            "parent": 263,
            "icon": ""
        },
        "10": {
            "id": 10,
            "name": "Food Products",
            "description": "Food & Beverage Company",
            "parent": 258,
            "icon": ""
        }};

You can see from above that I have elements in array where there is parent_id field.

Now basically I wanted to have a function which taking this input, create the modified version of this array where I will have the list of only elements which have any number of children.

That is if any element has parent_id without any children then in the list that element will not be added.

Elements which have children.

 var obj = { "categories": { "data": { "1": { "id": 1, "name": "Churches & Religious Org.", "description": "Church\\/religious organization", "parent": 267, "icon": "home" }, "2": { "id": 2, "name": "Bands & Musicians", "description": "Musician\\/band", "parent": 259, "icon": "music-note" }, "3": { "id": 3, "name": "Products & Services", "description": "Product\\/service", "parent": 260, "icon": "cube" }, "4": { "id": 4, "name": "Actors & Directors", "description": "Actor\\/director", "parent": 256, "icon": "film-marker" }, "5": { "id": 5, "name": "Athletes & Players", "description": "Athlete", "parent": 264, "icon": "ios-americanfootball" }, "6": { "id": 6, "name": "Movies", "description": "Movie", "parent": 256, "icon": "ios-film" }, "7": { "id": 7, "name": "TV Shows", "description": "TV show", "parent": 256, "icon": "ios-videocam" }, "8": { "id": 8, "name": "Professional Teams", "description": "Sports Team", "parent": 264, "icon": "" }, "9": { "id": 9, "name": "Politicians", "description": "Politician", "parent": 263, "icon": "" }, "10": { "id": 10, "name": "Food Products", "description": "Food & Beverage Company", "parent": 258, "icon": "" } } } } var elemetsWithChildren = []; for (var propt in obj.categories.data) { if (elemetsWithChildren.indexOf(obj.categories.data[propt].parent) == -1) { elemetsWithChildren.push(obj.categories.data[propt].parent); } } console.log(elemetsWithChildren); 

With this, you have an object with elements and numbers of children

var elemetsWithChildren = {};

        for(var propt in obj.categories.data){
            let parent = obj.categories.data[propt].parent
             if(!elemetsWithChildren.hasOwnProperty(parent)){
            elemetsWithChildren[parent] = 1;
             }else{
               elemetsWithChildren[parent]++;
             }
        }

console.log(elemetsWithChildren);

As commented; your "array" is not an array. You can use Object.values to turn it into an array. Then map that to only take parent value.

Now now have an array with values that are the id's of an item's parent. You can use these id's to create a new object with reduce, assuming that your object has a key that is it's id {id:{id:id:...},otherid:{id:otherid,...},...

 var data = { "1": { "id": 1, "name": "Churches & Religious Org.", "description": "Church\\/religious organization", "parent": 2, "icon": "home" }, "2": {//2 doesn't have a parent "id": 2, "name": "Bands & Musicians", "description": "Musician\\/band", "icon": "music-note" }, "3": { "id": 3, "name": "Products & Services", "description": "Product\\/service", "parent": 1, "icon": "cube" } }; console.log( Object.values(data) .map(d=>d.parent)//get only parent values .filter(parent=>parent)//remove those that don't have parent //reduce to a new object that only contains items that had other items // with it's id as their parent .reduce( (result,item)=>{ result[item]=data[item]; return result; }, {} ) ) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM