简体   繁体   English

如何在与父母一起创建数组时实现Javascript逻辑只有孩子

[英]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: 我通过以下方式具有父子格式的js对象数组:

"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. 从上面可以看到,我在有parent_id字段的数组中有元素。

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. 也就是说,如果任何元素具有不带任何子元素的parent_id,则在列表中将不会添加该元素。

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. 您可以使用Object.values将其转换为数组。 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. 现在有了一个数组,其值是项目父项的ID。 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,...},... 您可以使用这些ID来创建带有reduce的新对象,假设您的对象具有一个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; }, {} ) ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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