Home > Blockchain >  Unable to get blank properties and it's id from array of object
Unable to get blank properties and it's id from array of object

Time:08-16

i want to get empty properties(only need to check role,group and subgroup) and it's id both in array of objects.

let tempdata = [
        {
            "id": 41,
            "tool": "Artifactory",
            "role": "",
            "group": "Dish",
            "subgroup": "Ehub test 009",
            "subscriptionId": "artifactory-ehub-test-009"
        },
        {
            "id": 4,
            "tool": "Gitlab",
            "role": "Owner",
            "group": "IDP",
            "subgroup": "IDP-Service-Templates",
            "subscriptionId": "gitlab-51663585"
        }
    ]

What i tried so far is this:

tempdata.filter(item=>item.group=='' || item.subgroup=='' || item.role=='').map(item=>item.id)

but this only gives my id [41] what i want is [{"id":41,"blank_properties":["role"]}] Can somebody please help.

CodePudding user response:

you can simply do it this way

tempdata.map((item)=>{
    let d = [];
     if(item.role === ''){
    d.push('role')
     }
    if(item.group ===''){
        d.push('group')
    }
    if(item.subgroup===''){
        d.push('subgroup')
    }
    return {...item,'blank_prop':d}
})

CodePudding user response:

tempdata.filter(item=>item.group=='' || item.subgroup=='' || 
item.role=='').map(item=>{
let temp=[];
if(item.group==='')temp.push('group')
if(item.role==='')temp.push('role')
if(item.subgroup==='')temp.push('subgroup')
if(item.subscriptionId==='')temp.push('subscriptionId')
if(item.tool==='')temp.push('tool')
return {id:item.id,blank_property:temp};})

CodePudding user response:

I'm going to propose a more sophisticated solution, in case you're interested in additional ways to approach this problem:

let tempData = 
[
    {
        "id": 41,
        "tool": "Artifactory",
        "role": "",
        "group": "Dish",
        "subgroup": "Ehub test 009",
        "subscriptionId": "artifactory-ehub-test-009"
    },
    {
        "id": 4,
        "tool": "Gitlab",
        "role": "Owner",
        "group": "IDP",
        "subgroup": "IDP-Service-Templates",
        "subscriptionId": "gitlab-51663585"
    },
];

// An array of all properties you want to check for blank strings
const propertiesToCheck = [ "group", "subgroup", "role" ];

result = tempData
    .filter((item) =>
    {
        // Your original code was filtering the array of objects to
        //  JUST ones that have at least one of those properties set to ""
        //  So this filter does the same thing.
        //
        // If you DON'T actually want to outright remove ones that don't match this condition,
        //  then you can just remove this entire filter step.

        // Iterate object keys and values
        for (const [ key, value ] of Object.entries(item))
        {
            // If the key is not in the above array of propertiesToCheck,
            //  then skip it
            if (propertiesToCheck.indexOf(key) == -1)
            {
                continue;
            }

            // If we encounter one of those properties and it's blank, return true
            if (value == "")
            {
                return true;
            }
        }

        // Return false if we get through all of the properties without encountering one that's blank
        return false;
    })
    .map((item) =>
    {
        // Create an object to house the result in the manner you described
        const result =
        {
            id: item.id,
            blank_properties: [],
        };

        // Iterate the object keys and values again
        for (const [ key, value ] of Object.entries(item))
        {
            // Same deal as before
            if (propertiesToCheck.indexOf(key) == -1)
            {
                continue;
            }

            // Then, if the value is blank...
            if (value == "")
            {
                // ...push its key to the blank_properties array
                result.blank_properties.push(key);
            }
        }

        // Return the result!
        return result;
    });

// Prints: 
//  [ { id: 41, blank_properties: [ 'role' ] } ]
console.log(result);
  • Related