【问题标题】:Unable to get blank properties and it's id from array of object无法从对象数组中获取空​​白属性及其 id
【发布时间】:2022-08-16 21:41:38
【问题描述】:

我想获得空属性(只需要检查角色、组和子组),它的 id 都在对象数组中。

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\"
        }
    ]

到目前为止我尝试的是:

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

但这只会给我的 id [41] 我想要的是 [{\"id\":41,\"blank_properties\":[\"role\"]}] 有人可以帮忙吗?

    标签: javascript arrays


    【解决方案1】:

    你可以简单地这样做

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

    【讨论】:

      【解决方案2】:
      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};})
      

      【讨论】:

        【解决方案3】:

        如果您对解决此问题的其他方法感兴趣,我将提出一个更复杂的解决方案:

        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 it's 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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多