Ryan Hsin
Ryan Hsin

Reputation: 31

Javascript - How to create filtered nested Array

I have my array object here:

let businessList = [{
  name: "siAdmin",
  count: 52,
  children: [{
    name: "si1",
    count: 30,
    children: [{
      count: 10,
      name: "org1-1"
    }, {
      count: 14,
      name: "org1-2"
    }, {
      name: "org1-3",
      count: 6
    }]
  }, {
    name: "si2",
    count: 22,
    children: [{
      name: "org2-1",
      count: 22
    }]
  }]
}]

I want to make make a filtered list if count > 10, which would be like:

filteredList = [{
  name: "siAdmin",
  count: 52,
  children: [{
    name: "si1",
    count: 30,
    children: [{
      count: 14,
      name: "org1-2"
    }]
  }, {
    name: "si2",
    count: 22,
    children: [{
      name: "org2-1",
      count: 22
    }]
  }]
}]

How can I achieve this result?

And if condition changes to count > 23, it would be like:

filteredList = [{
  name: "siAdmin",
  count: 52,
  children: [{
    name: "si1",
    count: 30,
    children: []
  }]
}]

Upvotes: 2

Views: 94

Answers (3)

Ryan Hsin
Ryan Hsin

Reputation: 31

I finally find the solution without breaking the original object, here's my code:

let businessList = [{
  name: "siAdmin",
  count: 52,
  children: [{
    name: "si1",
    count: 30,
    children: [{
      count: 10,
      name: "org1-1"
    }, {
      count: 14,
      name: "org1-2"
    }, {
      name: "org1-3",
      count: 6
    }]
  }, {
    name: "si2",
    count: 22,
    children: [{
      name: "org2-1",
      count: 22
    }]
  }]
}];

function filteredBusiness(businessArr, count) {
  let arr = [];
  businessArr.forEach((business) => {
    let index = 0;
    let businessMatched = business.count > count
    if (businessMatched) {
      let {
        children,
        ...dataWithoutChildren
      } = business;
      arr.push({
        ...dataWithoutChildren
      });
      index = arr.length - 1;
      let hasChildren = business.children;
      if (hasChildren) {
        // arr[index].children = [];
        //check children matched
        let nextBusinessArr = filteredBusiness(
          business.children,
          count
        );
        if (nextBusinessArr) {
          arr[index].children = nextBusinessArr;
        }
      }
    }
  })
  return arr;
}
console.log(filteredBusiness(businessList, 10))

Upvotes: 0

Md Sabbir Alam
Md Sabbir Alam

Reputation: 5054

You can use recursion like this,

let businessList = [{
  name: "siAdmin",
  count: 52,
  children: [{
    name: "si1",
    count: 30,
    children: [{
      count: 10,
      name: "org1-1"
    }, {
      count: 14,
      name: "org1-2"
    }, {
      name: "org1-3",
      count: 6
    }]
  }, {
    name: "si2",
    count: 22,
    children: [{
      name: "org2-1",
      count: 22
    }]
  }]
}];

const filteredArray = (arr, countLimit) => {
  return arr.filter(item => {
    if(item.hasOwnProperty('children')) {
      item.children = filteredArray(item.children, countLimit);
    }
    return item.count > countLimit;
  })
};

console.log(filteredArray(JSON.parse(JSON.stringify(businessList)), 10));
console.log(filteredArray(JSON.parse(JSON.stringify(businessList)), 23));

console.log(businessList);

Upvotes: 3

Abhishek
Abhishek

Reputation: 89

    let businessList = [{
  name: "siAdmin",
  count: 52,
  children: [{
    name: "si1",
    count: 30,
    children: [{
      count: 10,
      name: "org1-1"
    }, {
      count: 14,
      name: "org1-2"
    }, {
      name: "org1-3",
      count: 6
    }]
  }, {
    name: "si2",
    count: 22,
    children: [{
      name: "org2-1",
      count: 22
    }]
  }]
}]

function getData(input, number){
    let len = input.length;
    let ans = [];
    for(let i = 0; i < len; i++){
        if(input[i].count >= number){
            if(input[i].children){
                let data = getData(input[i].children,number);
            //ans.push(input[i]);
            input[i].children = data;
            ans.push(input[i])
            }
            else{
                ans.push(input[i]);
            }
        }
    }
    return ans;
}
getData(businessList,10)
getData(businessList,23)

above function should work

Upvotes: 1

Related Questions