I am trying to get the count of the unique item from the array of objects. I have a dataset where I am having a array of objects inside that I am having a nested array of objects, I want to get the count of the two objects, I have taken the count of the one object based on the condition but I am unable to do the nested filter and get the count of the unique item. Could any one assist me what I need to achieve the same. Thanks in advance.
MockData:
[
{
id: "123",
type: "A",
desc: "To be added",
inProcessing: "true",
isValidated: "false",
status: { type: "in-progress" },
subCatg: [
{
id: "A-1",
status: {
type: "YTC",
},
type: "A-01",
inProcessing: "true",
isValidated: "false",
},
{
id: "A-2",
status: {
type: "YTC",
},
type: "A-02",
inProcessing: "true",
isValidated: "false",
},
],
},
{
id: "124",
type: "A",
desc: "To be added",
inProcessing: "true",
isValidated: "false",
status: { type: "in-progress" },
subCatg: [],
},
];
In the above data, I need to get the count of object and nested array of object subCatg
. I've added the logic to get the count of object but I am unable to get the count of subcatg
I need the count of both.
Code:
getCount = (itemstoBeCreated) => {
const { typeCreated } = this.props; // props coming from anothe comp
return (
typeCreated itemstoBeCreated.filter((items) => !items.inProcessing && isValidated === true).length
);
};
I tried to get the count of subCtg
with same condition but I am unable to get output.
CodePudding user response:
is this what you're looking?
const mockdata = [{
id: "123",
type: "A",
desc: "To be added",
inProcessing: "true",
isValidated: "false",
status: {
type: "in-progress"
},
subCatg: [{
id: "A-1",
status: {
type: "YTC",
},
type: "A-01",
inProcessing: "true",
isValidated: "false",
},
{
id: "A-2",
status: {
type: "YTC",
},
type: "A-02",
inProcessing: "true",
isValidated: "false",
},
],
},
{
id: "124",
type: "A",
desc: "To be added",
inProcessing: "true",
isValidated: "false",
status: {
type: "in-progress"
},
subCatg: [],
},
];
const result = mockdata.reduce((p, c) => p = c.subCatg.filter(cs => !cs.inProcessing && cs.isValidated === true).length, 0);
console.log(result);
CodePudding user response:
let x = [
{
id: "123",
type: "A",
desc: "To be added",
inProcessing: true, // hopping that you want bool value here
isValidated: false, // if its string then check your "Deciding Condition" to be equal with "true" or "false" as string
status: { type: "in-progress" },
subCatg: [
{
id: "A-1",
status: {
type: "YTC",
},
type: "A-01",
inProcessing: true,
isValidated: false,
},
{
id: "A-2",
status: {
type: "YTC",
},
type: "A-02",
inProcessing: true,
isValidated: false,
},
],
},
{
id: "124",
type: "A",
desc: "To be added",
inProcessing: true,
isValidated: false,
status: { type: "in-progress" },
subCatg: [],
},
];
let result=0 // make sure you place this variable before function as it stores count and start with 0
let count = (array) => {
let subCount = array.filter((item,i) => {
(item.subCatg && item.subCatg.length > 0) ? count(item.subCatg) : null;
// return Write your "Deciding Condition"here after return true result of condition will be counted in result variable
// For example
//return (item.inProcessing && (item.isValidated===false)) //this will return 4 as result becouse inProcessing is true and checking isValidated===false returns true so true&&true gets true hence counted
return (!item.inProcessing && item.isValidated)===false //this will return 4 as result becouse inprocess is true and !true is false and isValidated is false so false&&false will return true hence counted
// make sure you apply your condition properly
// NOTE : I HAVE CONSIDER VALUE Of inProcessing and isValidated as typeof "bool" if you expect them to be string refer following condition
// return (!item.inProcessing==='true' && item.isValidated==='false') this will return 4 as result. hope you get it
}).length
result =subCount
}
count(x) // calling the function with your data as parameter
console.log(result) //your output
CodePudding user response:
If I'm understanding you correctly based on the question and the comments, you need the total count of objects filtered by the inProcessing
and isValidated
properties. So in the example data, that would match 2 objects.
If that's the case, you can achieve this pretty easily with a little recursion.
const data = [
{
id: "123",
type: "A",
desc: "To be added",
inProcessing: "true",
isValidated: "false",
status: { type: "in-progress" },
subCatg: [
{
id: "A-1",
status: {
type: "YTC",
},
type: "A-01",
inProcessing: "true",
isValidated: "false",
},
{
id: "A-2",
status: {
type: "YTC",
},
type: "A-02",
inProcessing: "true",
isValidated: "false",
},
],
},
{
id: "124",
type: "A",
desc: "To be added",
inProcessing: "true",
isValidated: "false",
status: { type: "in-progress" },
subCatg: [],
},
];
function recurseData(data) {
let count = data.length;
data.forEach(obj => {
// strings are truthy values so we parse it to get the boolean.
const inProcessing = JSON.parse(obj.inProcessing);
const isValidated = JSON.parse(obj.isValidated);
const shouldCount = !inProcessing && isValidated && obj.subCatg;
if (shouldCount) {
count = recurseData(obj.subCatg);
}
})
return count;
}
const count = recurseData(data);
console.log({ count });