Home > other >  find all child from an array and create new Array in javascript
find all child from an array and create new Array in javascript

Time:02-23

I have one array as below :

[
  {
    id: '97b76d27-ad1d-42f9-89e9-be8ca61dc91d',
    name: 'nokia',
    filename: '1644992710452-698281553portland_skyline.jpg',
    parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
  },
  {
    id: 'dc1be5de-c871-475e-b9f7-a404c9625fb9',
    name: 'realme',
    filename: '1644992710452-698281553portland_skyline.jpg',
    parent_id: '8016c51f-fcc5-44f5-8492-d90a88ecfd7a'
  },
  {
    id: 'fa542e2b-7ac2-4979-b13d-0ebf57b08ddf',
    name: 'oneplus',
    filename: '1644992710452-698281553portland_skyline.jpg',
    parent_id: '97b76d27-ad1d-42f9-89e9-be8ca61dc91d'
  },
  {
    id: '8016c51f-fcc5-44f5-8492-d90a88ecfd7a',
    name: 'Galaxy prime',
    filename: '1644992710452-698281553portland_skyline.jpg',
    parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
  },
  {
    id: 'e66b4903-6f5d-4311-b9c9-2efd6750a776',
    name: 'Galaxy tab',
    filename: '1645512038535-438816287Vanamo_Logo.png',
    parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
  },
  {
    id: 'a725e322-5c0c-4443-9ad2-9747d41b283e',
    name: 'Galaxy champ',
    filename: '1645512305563-841288453Vanamo_Logo.png',
    parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
  },
  {
    id: 'b1132090-ad75-4f19-b449-3595ae1620c2',
    name: 'samsung',
    filename: '1645527489139-708675878Vanamo_Logo.png',
    parent_id: null
  },
  {
    id: '49ef97df-9066-458a-9794-7798ee4d72c1',
    name: 'note 9 pro',
    filename: '1645592972776-366584498Vanamo_Logo.png',
    parent_id: '41a7aac1-1957-496a-86bb-7b53768c0a35'
  },
  {
    id: '395b595e-6b23-43ab-9cf4-eca495901c75',
    name: 'Xiomi',
    filename: '1645594875297-864602535Vanamo_Logo.png',
    parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
  },
  {
    id: 'daa69323-ee9e-4ff6-87f5-fd053656ba35',
    name: 'note 8',
    filename: '1645594891221-11318065Vanamo_Logo.png',
    parent_id: '395b595e-6b23-43ab-9cf4-eca495901c75'
  },
  {
    id: '0290bb2b-38ab-4d97-b3e8-5d7af471eff8',
    name: 'note 9',
    filename: '1645594910725-698567441Vanamo_Logo.png',
    parent_id: '395b595e-6b23-43ab-9cf4-eca495901c75'
  },
  {
    id: '0035b5b4-a0ae-4f00-9b0a-c8bd33d159ee',
    name: 'note 8 pro',
    filename: '1645594924778-936353966Vanamo_Logo.png',
    parent_id: 'daa69323-ee9e-4ff6-87f5-fd053656ba35'
  },
  {
    id: '40ab3cfd-52c4-4e7e-87e0-79133571c934',
    name: 'note 9 pro',
    filename: '1645594939585-660548801Vanamo_Logo.png',
    parent_id: '0290bb2b-38ab-4d97-b3e8-5d7af471eff8'
  }
]

If I filter array with xiomi id = 395b595e-6b23-43ab-9cf4-eca495901c75.

I am only able to get two child. Because, xiomi id is parent_id of note 8 and note 9.

[
  {
    id: 'daa69323-ee9e-4ff6-87f5-fd053656ba35',
    name: 'note 8',
    filename: '1645594891221-11318065Vanamo_Logo.png',
    parent_id: '395b595e-6b23-43ab-9cf4-eca495901c75'
  },
  {
    id: '0290bb2b-38ab-4d97-b3e8-5d7af471eff8',
    name: 'note 9',
    filename: '1645594910725-698567441Vanamo_Logo.png',
    parent_id: '395b595e-6b23-43ab-9cf4-eca495901c75'
  }
]

but note 8 has child note 8 pro and note 9 has child note 9 pro.

how can I get that output?

Below is My Code :

let orgList = []
    childOrgs.filter((item) => {
      orgList.push({
        id : item.dataValues.id,
        name : item.dataValues.name,
        filename : item.dataValues.logo_filename,
        parent_id : item.dataValues.parent_org_id
      })
    })
    console.log('<<-- orgList -->>', orgList);
    orgList = orgList.filter((orgs)=> orgs.parent_id === org[0].dataValues.id);
    console.log('<<---- orgList ---->>', orgList);
    const result = await organizationService.deleteOrg(id);
    // fs.unlinkSync(`../../../static/${filename}`, (err) => { console.log('err -->>',err)});
    console.log("result -->> ", result);
    if (result === 1) {
      res
        .status(200)
        .json({ message: "Organization deleted successfully!", success: true });
    } else {
      res
        .status(400)
        .json({ message: "something went wrong!", success: false });
    }

Please help me to sort it out. Thanks:)

CodePudding user response:

var list = [
    {
        id: "97b76d27-ad1d-42f9-89e9-be8ca61dc91d",
        name: "nokia",
        filename: "1644992710452-698281553portland_skyline.jpg",
        parent_id: "b1132090-ad75-4f19-b449-3595ae1620c2",
    },
    {
        id: "dc1be5de-c871-475e-b9f7-a404c9625fb9",
        name: "realme",
        filename: "1644992710452-698281553portland_skyline.jpg",
        parent_id: "8016c51f-fcc5-44f5-8492-d90a88ecfd7a",
    },
    {
        id: "fa542e2b-7ac2-4979-b13d-0ebf57b08ddf",
        name: "oneplus",
        filename: "1644992710452-698281553portland_skyline.jpg",
        parent_id: "97b76d27-ad1d-42f9-89e9-be8ca61dc91d",
    },
    {
        id: "8016c51f-fcc5-44f5-8492-d90a88ecfd7a",
        name: "Galaxy prime",
        filename: "1644992710452-698281553portland_skyline.jpg",
        parent_id: "b1132090-ad75-4f19-b449-3595ae1620c2",
    },
    {
        id: "e66b4903-6f5d-4311-b9c9-2efd6750a776",
        name: "Galaxy tab",
        filename: "1645512038535-438816287Vanamo_Logo.png",
        parent_id: "b1132090-ad75-4f19-b449-3595ae1620c2",
    },
    {
        id: "a725e322-5c0c-4443-9ad2-9747d41b283e",
        name: "Galaxy champ",
        filename: "1645512305563-841288453Vanamo_Logo.png",
        parent_id: "b1132090-ad75-4f19-b449-3595ae1620c2",
    },
    {
        id: "b1132090-ad75-4f19-b449-3595ae1620c2",
        name: "samsung",
        filename: "1645527489139-708675878Vanamo_Logo.png",
        parent_id: null,
    },
    {
        id: "49ef97df-9066-458a-9794-7798ee4d72c1",
        name: "note 9 pro",
        filename: "1645592972776-366584498Vanamo_Logo.png",
        parent_id: "41a7aac1-1957-496a-86bb-7b53768c0a35",
    },
    {
        id: "395b595e-6b23-43ab-9cf4-eca495901c75",
        name: "Xiomi",
        filename: "1645594875297-864602535Vanamo_Logo.png",
        parent_id: "b1132090-ad75-4f19-b449-3595ae1620c2",
    },
    {
        id: "daa69323-ee9e-4ff6-87f5-fd053656ba35",
        name: "note 8",
        filename: "1645594891221-11318065Vanamo_Logo.png",
        parent_id: "395b595e-6b23-43ab-9cf4-eca495901c75",
    },
    {
        id: "0290bb2b-38ab-4d97-b3e8-5d7af471eff8",
        name: "note 9",
        filename: "1645594910725-698567441Vanamo_Logo.png",
        parent_id: "395b595e-6b23-43ab-9cf4-eca495901c75",
    },
    {
        id: "0035b5b4-a0ae-4f00-9b0a-c8bd33d159ee",
        name: "note 8 pro",
        filename: "1645594924778-936353966Vanamo_Logo.png",
        parent_id: "daa69323-ee9e-4ff6-87f5-fd053656ba35",
    },
    {
        id: "40ab3cfd-52c4-4e7e-87e0-79133571c934",
        name: "note 9 pro",
        filename: "1645594939585-660548801Vanamo_Logo.png",
        parent_id: "0290bb2b-38ab-4d97-b3e8-5d7af471eff8",
    },
];

function findAllChild(id, arr) {
    list.filter((child) => child.parent_id === id).map((item) => {
        arr.push(item);
        findAllChild(item.id, arr);
    });
    return arr;
}

const searchId = "395b595e-6b23-43ab-9cf4-eca495901c75";

const childArray = findAllChild(searchId, []);
console.log(childArray);

CodePudding user response:

Search for both id and parent_id

const list = [
  {
    id: '97b76d27-ad1d-42f9-89e9-be8ca61dc91d',
    name: 'nokia',
    filename: '1644992710452-698281553portland_skyline.jpg',
    parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
  },
  {
    id: 'dc1be5de-c871-475e-b9f7-a404c9625fb9',
    name: 'realme',
    filename: '1644992710452-698281553portland_skyline.jpg',
    parent_id: '8016c51f-fcc5-44f5-8492-d90a88ecfd7a'
  },
  {
    id: 'fa542e2b-7ac2-4979-b13d-0ebf57b08ddf',
    name: 'oneplus',
    filename: '1644992710452-698281553portland_skyline.jpg',
    parent_id: '97b76d27-ad1d-42f9-89e9-be8ca61dc91d'
  },
  {
    id: '8016c51f-fcc5-44f5-8492-d90a88ecfd7a',
    name: 'Galaxy prime',
    filename: '1644992710452-698281553portland_skyline.jpg',
    parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
  },
  {
    id: 'e66b4903-6f5d-4311-b9c9-2efd6750a776',
    name: 'Galaxy tab',
    filename: '1645512038535-438816287Vanamo_Logo.png',
    parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
  },
  {
    id: 'a725e322-5c0c-4443-9ad2-9747d41b283e',
    name: 'Galaxy champ',
    filename: '1645512305563-841288453Vanamo_Logo.png',
    parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
  },
  {
    id: 'b1132090-ad75-4f19-b449-3595ae1620c2',
    name: 'samsung',
    filename: '1645527489139-708675878Vanamo_Logo.png',
    parent_id: null
  },
  {
    id: '49ef97df-9066-458a-9794-7798ee4d72c1',
    name: 'note 9 pro',
    filename: '1645592972776-366584498Vanamo_Logo.png',
    parent_id: '41a7aac1-1957-496a-86bb-7b53768c0a35'
  },
  {
    id: '395b595e-6b23-43ab-9cf4-eca495901c75',
    name: 'Xiomi',
    filename: '1645594875297-864602535Vanamo_Logo.png',
    parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
  },
  {
    id: 'daa69323-ee9e-4ff6-87f5-fd053656ba35',
    name: 'note 8',
    filename: '1645594891221-11318065Vanamo_Logo.png',
    parent_id: '395b595e-6b23-43ab-9cf4-eca495901c75'
  },
  {
    id: '0290bb2b-38ab-4d97-b3e8-5d7af471eff8',
    name: 'note 9',
    filename: '1645594910725-698567441Vanamo_Logo.png',
    parent_id: '395b595e-6b23-43ab-9cf4-eca495901c75'
  },
  {
    id: '0035b5b4-a0ae-4f00-9b0a-c8bd33d159ee',
    name: 'note 8 pro',
    filename: '1645594924778-936353966Vanamo_Logo.png',
    parent_id: 'daa69323-ee9e-4ff6-87f5-fd053656ba35'
  },
  {
    id: '40ab3cfd-52c4-4e7e-87e0-79133571c934',
    name: 'note 9 pro',
    filename: '1645594939585-660548801Vanamo_Logo.png',
    parent_id: '0290bb2b-38ab-4d97-b3e8-5d7af471eff8'
  }
]

const searchId = "395b595e-6b23-43ab-9cf4-eca495901c75";

const output = list.filter((node) => node.id === searchId || node.parent_id === searchId);
console.log(output);

CodePudding user response:

I have refactor your code:

const orgList = childOrgs.map((item) => ({
        id : item.dataValues.id,
        name : item.dataValues.name,
        filename : item.dataValues.logo_filename,
        parent_id : item.dataValues.parent_org_id
    }))

    console.log('<<-- orgList -->>', orgList);
    const orgListFilter = orgList.filter((orgs)=> orgs.parent_id === org[0].dataValues.id);
    const orgListFilterChild = orgList.filter((orgs)=> !!orgListFilter.find(parent => orgs.parent_id === parent.id));
    const resultOrgList = [...orgListFilter, ...orgListFilterChild]
    console.log('<<---- resultOrgList ---->>', resultOrgList);
    ...

CodePudding user response:

If you want to append the corresponding childs too then you can use this approach and append their childs too if you want to return only childs then you can simply change the logic of i.childs... and can return the array of all childs

  const mobiles=[
    {
      id: '97b76d27-ad1d-42f9-89e9-be8ca61dc91d',
      name: 'nokia',
      filename: '1644992710452-698281553portland_skyline.jpg',
      parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
    },
    {
      id: 'dc1be5de-c871-475e-b9f7-a404c9625fb9',
      name: 'realme',
      filename: '1644992710452-698281553portland_skyline.jpg',
      parent_id: '8016c51f-fcc5-44f5-8492-d90a88ecfd7a'
    },
    {
      id: 'fa542e2b-7ac2-4979-b13d-0ebf57b08ddf',
      name: 'oneplus',
      filename: '1644992710452-698281553portland_skyline.jpg',
      parent_id: '97b76d27-ad1d-42f9-89e9-be8ca61dc91d'
    },
    {
      id: '8016c51f-fcc5-44f5-8492-d90a88ecfd7a',
      name: 'Galaxy prime',
      filename: '1644992710452-698281553portland_skyline.jpg',
      parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
    },
    {
      id: 'e66b4903-6f5d-4311-b9c9-2efd6750a776',
      name: 'Galaxy tab',
      filename: '1645512038535-438816287Vanamo_Logo.png',
      parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
    },
    {
      id: 'a725e322-5c0c-4443-9ad2-9747d41b283e',
      name: 'Galaxy champ',
      filename: '1645512305563-841288453Vanamo_Logo.png',
      parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
    },
    {
      id: 'b1132090-ad75-4f19-b449-3595ae1620c2',
      name: 'samsung',
      filename: '1645527489139-708675878Vanamo_Logo.png',
      parent_id: null
    },
    {
      id: '49ef97df-9066-458a-9794-7798ee4d72c1',
      name: 'note 9 pro',
      filename: '1645592972776-366584498Vanamo_Logo.png',
      parent_id: '41a7aac1-1957-496a-86bb-7b53768c0a35'
    },
    {
      id: '395b595e-6b23-43ab-9cf4-eca495901c75',
      name: 'Xiomi',
      filename: '1645594875297-864602535Vanamo_Logo.png',
      parent_id: 'b1132090-ad75-4f19-b449-3595ae1620c2'
    },
    {
      id: 'daa69323-ee9e-4ff6-87f5-fd053656ba35',
      name: 'note 8',
      filename: '1645594891221-11318065Vanamo_Logo.png',
      parent_id: '395b595e-6b23-43ab-9cf4-eca495901c75'
    },
    {
      id: '0290bb2b-38ab-4d97-b3e8-5d7af471eff8',
      name: 'note 9',
      filename: '1645594910725-698567441Vanamo_Logo.png',
      parent_id: '395b595e-6b23-43ab-9cf4-eca495901c75'
    },
    {
      id: '0035b5b4-a0ae-4f00-9b0a-c8bd33d159ee',
      name: 'note 8 pro',
      filename: '1645594924778-936353966Vanamo_Logo.png',
      parent_id: 'daa69323-ee9e-4ff6-87f5-fd053656ba35'
    },
    {
      id: '40ab3cfd-52c4-4e7e-87e0-79133571c934',
      name: 'note 9 pro',
      filename: '1645594939585-660548801Vanamo_Logo.png',
      parent_id: '0290bb2b-38ab-4d97-b3e8-5d7af471eff8'
    }
  ];



  function makeChild(mobiles,searchId){
    let items=mobiles.filter(item=>item.id===searchId);

    if(items.length) {
      let nodes=searchNode(items);
      return nodes;
    }
    return null;
  }
  function searchNode(items){
    return items.map((i)=>{
      let items_2=mobiles.filter(item=>item.parent_id===i.id);
      if(items_2.length) {
        i.childs=searchNode(items_2);
      }
      return i
    })
  
  }
  console.log(makeChild(mobiles,'395b595e-6b23-43ab-9cf4-eca495901c75'))

  • Related