2

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: []
  }]
}]
2
  • What did you try share with us Commented Dec 13, 2020 at 8:48
  • Shell this si2 be in children? Commented Dec 13, 2020 at 9:00

3 Answers 3

3

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);

Sign up to request clarification or add additional context in comments.

3 Comments

this is not working becauses it will mess up original businessList in item.children = filteredArray(item.children, countLimit) part. I think the push method is the only solution for me.
You can create a new copy of businessList and then filter that copy array to keep your businessList intact.
@RyanHsin updated the code please take a look.
1
    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

9 Comments

Oh damn use forEach or map
@Alex I avoid for each or map bacuse there would be more function call other than recursion which might effect stack
Your code will produce wrong answer as the logic should be count>givenNumber not count>=givenNumber as mentioned in the question. for getData(input, 10) there should be no item with count = 10.
@sabbir.alam thanks for pointing it out, I assumed those things can be handled once logic is clear
Besides this code will not scale if there are more than 2 level of nested children.
|
0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.