Home > Enterprise >  Search inside child Array of objects with multiple value
Search inside child Array of objects with multiple value

Time:11-20

Currently i have below Array of Objects

const dataClass = [
    {
        "id": 101,
        "class": [
            {
                "type": "A",
                "value": "A-class"
            },
            {
                "type": "B",
                "value": "B-class"
            },
            {
                "type": "C",
                "value": "C-class"
            }
        ],
        "rank": 1
    },
    {
        "id": 102,
        "class": [
            {
                "type": "D",
                "value": "D-class"
            },
            {
                "type": "E",
                "value": "E-class"
            },
            {
                "type": "F",
                "value": "F-class"
            }
        ],
        "rank": 2
    },
    {
        "id": 103,
        "class": [
            {
                "type": "G",
                "value": "G-class"
            },
            {
                "type": "H",
                "value": "H-class"
            },
            {
                "type": "I",
                "value": "I-class"
            }
        ],
        "rank": 3
    }
];

i need to get dataClass object using all value inside the class object, let say i want to get the second object, so i have to search/input "type": "D", "type": "E", and "type": "F".

return array object/object i expect:

    [{
        "id": 102,
        "class": [
            {
                "type": "D",
                "value": "D-class"
            },
            {
                "type": "E",
                "value": "E-class"
            },
            {
                "type": "F",
                "value": "F-class"
            }
        ],
        "rank": 2
    }]

I don't find any solution so far, Thanks for any help.

CodePudding user response:

Hope this will help,

const dataClass = [
    {
        "id": 101,
        "class": [
            {
                "type": "A",
                "value": "A-class"
            },
            {
                "type": "B",
                "value": "B-class"
            },
            {
                "type": "C",
                "value": "C-class"
            }
        ],
        "rank": 1
    },
    {
        "id": 102,
        "class": [
            {
                "type": "D",
                "value": "D-class"
            },
            {
                "type": "E",
                "value": "E-class"
            },
            {
                "type": "F",
                "value": "F-class"
            }
        ],
        "rank": 2
    },
    {
        "id": 103,
        "class": [
            {
                "type": "G",
                "value": "G-class"
            },
            {
                "type": "H",
                "value": "H-class"
            },
            {
                "type": "I",
                "value": "I-class"
            }
        ],
        "rank": 3
    }
];

const resultArr = [];

for (const ch_arr of dataClass){
  for (const class_arr of ch_arr["class"]){
      if(["D","E","F"].includes(class_arr["type"])){
        resultArr.push(ch_arr);
        break;
      }
    };
};

// resultArr is the expected array

CodePudding user response:

You need find the object inside of class Array so i think using find method is the more readable way to solved it

function findClassByType(value: string) {
  return [dataClass.find((obj) => obj.class.find(({ type }) => type.toLocaleLowerCase() === value.toLocaleLowerCase()))];
}
    
console.log(findClassByType('a'))

I added the toLocaleLowerCase to avoid case sensitive.

CodePudding user response:

I added one more object with types D, E, F at rank 4

If you want to return all objects that match your filtration, check result1

and if you just wanna return the first object that matches, check result2

const dataClass = [
    {
        "id": 101,
        "class": [
            {
                "type": "A",
                "value": "A-class"
            },
            {
                "type": "B",
                "value": "B-class"
            },
            {
                "type": "C",
                "value": "C-class"
            }
        ],
        "rank": 1
    },
    {
        "id": 102,
        "class": [
            {
                "type": "D",
                "value": "D-class"
            },
            {
                "type": "E",
                "value": "E-class"
            },
            {
                "type": "F",
                "value": "F-class"
            }
        ],
        "rank": 2
    },
    {
        "id": 103,
        "class": [
            {
                "type": "G",
                "value": "G-class"
            },
            {
                "type": "H",
                "value": "H-class"
            },
            {
                "type": "I",
                "value": "I-class"
            }
        ],
        "rank": 3
    },
    {
        "id": 104,
        "class": [
            {
                "type": "D",
                "value": "D-class"
            },
            {
                "type": "E",
                "value": "E-class"
            },
            {
                "type": "F",
                "value": "F-class"
            }
        ],
        "rank": 4
    }
];


const expectedValues = ['D', 'E', 'F'];

//use this if you wanna return all objects that match expectedValues
const result1 = dataClass.filter(el => el.class.every(obj => expectedValues.includes(obj.type)));

console.log('all matched Objects => ', result1);

//use this if you wanna return the first object that match expectedValues
const result2 = dataClass.find(el => el.class.every(obj => expectedValues.includes(obj.type)));

console.log('first matched object => ',result2);

  • Related