score:1
how can i handle the above data with no inner children array for children of type "TYPE2".
You could add a guard clause to your loop
function:
if (!children) return;
Which returns directly if there are no children.
Resulting in:
const findCount = (arr_obj) => {
let count = 0;
const expectedCount = 2;
const loop = (children) => {
if (!children) return;
for (const obj of children) {
const { type, children } = obj;
if (type === 'TYPE2') {
loop(children);
} else if (type === 'MAIN') {
++count;
}
}
};
loop(arr_obj); // <- this should probably refer to `arr_obj`
return count > expectedCount;
};
const output = findCount(arr_obj);
score:0
For no inner children, you can check if the array of children has any elements in it or not. Something like this
const loop = (children) => {
for (const obj of children) {
const { type, children } = obj;
if (type === 'TYPE2' && children.length > 0) {
loop(children);
} else if (type === 'MAIN') {
++count;
}
}
};
score:0
let count = 0;
arr_obj.?.filter(item=> item.type =="TYPE2").forEach((item)=> {
if(item.children.length > 0){
item.children.forEach((childItem)=>{
if(childItem.type == "MAIN"){
count+=1;
}
})
}
})
console.log(count);// output will be 6
score:0
I modified the example by adding another valid occurrence in the nested object.
The following would also work.
// count the occurences of typeX as the very next of typeY
const count_TypeX_Within_TypeY = (arr, typeX, typeY) => {
let count = 0;
arr.forEach((item) => {
// check the type in current level
if (item.type === typeY) {
item.children.forEach((innerItem) => {
// // check the type in next level
if (innerItem.type === typeX) {
count += 1;
}
});
}
// do the same recursively
count += count_TypeX_Within_TypeY(item.children || [], typeX, typeY);
});
return count;
};
const arr_obj = [{"id":"1","children":[],"type":"TYPE1"},{"id":"2","children":[{"id":"1","children":[{}],"type":"MAIN"},{"id":"2","children":[{}],"type":"MAIN"},{"id":"3","children":[{}],"type":"MAIN"}],"type":"TYPE2"},{"id":"3","children":[{"id":"4","children":[{}],"type":"MAIN"},{"id":"5","children":[{"id":"7","type":"TYPE2","children":[{"id":"8","type":"MAIN","children":[{}]}]}],"type":"MAIN"},{"id":"6","children":[{}],"type":"MAIN"}],"type":"TYPE2"}];
console.log(count_TypeX_Within_TypeY(arr_obj, "MAIN", "TYPE2"));
Source: stackoverflow.com
Related Query
- How to find the count of some object property in array of objects using react and javascript?
- How to find if one of the items have completed property with value false from an array of objects using javascript and react?
- How to send array of objects data from child component to parent component and store in the parent object using react hooks?
- How to find an object from recursive array of objects that is matching with the id using javascript and react?
- How to check if the array of object is null using react and javascript?
- Using React hooks, how to update the state of part of an object in an array of objects
- How to count object property occurrence in an array by adding count property to the object properties and updating each occurrence of count?
- How to map through array of objects and create a new array of objects with a new added property using react and javascript?
- How to fetch an array of objects and render it in the component as a normal text using React Hooks?
- How to recursively loop through array of objects and find the index of matching id using javascript and react?
- How to delete object from array using object property - React
- How to find object in array and show it in React component?
- How do I correctly add data from multiple endpoints called inside useEffect to the state object using React Hooks and Context API?
- React how to conditionally edit or add object in array of objects using setState
- How to update the array of objects using onChange handler in React JS?
- How to find most recent date from an array of object then add new field and return array in react
- Counting duplicates in object array and storing the count as a new object have error !!! react js
- how do i update state in react for updating the value of an object inside an array using array.map function
- React Hooks, useState related question. How to update one property of object and keep the rest
- How to check if property of an objects of array matches with one of the values in another array of object
- How to return an object property from an array of objects in react
- How to change property of one object in an array of objects in React state?
- How do I iterate though an array of objects with keys and values in react and render them in the JSX( Both keys and Values)
- In React How can I fetch data and render a single object in a states array without mapping the whole thing?
- How to loop through an array an put create an array of objects using react and javascript?
- Best way to group an array with property of object and how to render the result
- How to fix the count variable value in a recursive method using react and javascript?
- How to fix the error object is possibly null or undefined using react and typescript?
- How should I update both array and <ul> at the same time using react hooks?
- How to extract only values from array of objects and put it to an array using react and javascript?
More Query from same tag
- Correct way of Creating multiple stores with mobx and injecting it into to a Component - ReactJs
- How to display table sort up/down arrows properly?
- React Warning: Cannot update during an existing state transition. Search component
- How could I send a React.js component from my node.js to my client?
- reactjs checkbox check and unchecked event
- Trying to change react Carousel into class component
- this.prop trampling in textarea change handler
- How to add defaultprops or value in react
- Why is the MuiThemeProvider overriding a component style?
- TypeError: messagesRef.child is not a function
- How can I use different properties for the same component using Styled Component for React JS?
- Passing props to component after declaration in reactjs
- How to retrieve images from server path using reactjs
- JSON.Stringify | Output the result on the screen with quote tags ''
- React Fetch to Laravel API Creates New Session
- How to trigger Reactjs onClick inside render instead of return?
- Compile error when running app with cordova after installing firebase plugins
- Testing Flux store with async api using Jasmine
- React dependency injection or similar?
- React - Why this function in onClick doesn't work?
- ReactJs Image Not found on production mode
- Get React DatePicker date value in onChange
- React + Redux for a complex SaaS
- Code not executing in order (Firebase). Synchronization issues
- React component not re-rendering on URL parameter change when using useEffect hook to fetch data
- How do you incrementally update a list in React?
- Need to check array of object in React.js
- Material-UI TextField variant label striking through value
- Get multiple entries for parent object foreach child (this child is array) object within that parent
- React Component doesn't rerender after sorting array in store