简体   繁体   中英

How to return few values using map

I have function that loops array and I have four if's - if it match I push value to output array:

const generate = (resources, resourceId) => {
    let output = [];

    for (let i = 0; i < resources.length; i++) {
        if (resources[i].id === resourceId) {
            if (CREATE & resources[i].privileges) {
                output.push(CREATE);
            }

            if (READ & resources[i].privileges) {
                output.push(READ);
            }

            if (UPDATE & resources[i].privileges) {
                output.push(UPDATE);
            }

            if (DELETE & resources[i].privileges) {
                output.push(DELETE);
            }
        }
    }

    return output;
};

I want to change this function to use map - is it possible? I try to do something like this:

const generateUsingMap = (resources, resourceId) => {
    return resources.map((resource) => {
        if (resource.id === resourceId) {
            if (CREATE & resource.privileges) {
                return CREATE;
            }
    
            if (READ & resource.privileges) {
                return READ;
            }
    
            if (UPDATE & resource.privileges) {
                return UPDATE;
            }
    
            if (UPDATE & resource.privileges) {
                return UPDATE;
            }
        }
    });
};

But in this case I will have only one value, because it returns from first if. Maybe I need to use another function? I don't want to use for or forEach because in that cases I need to create unnecessary variable.

Update

My function is working in loop, function receive 2 arguments resources and resourceId . For example variable resources contains:

  {
    "id": "1",
    "name": "Test name 1",
    "privileges": 1
  },
  {
    "id": "2",
    "name": "Test name 2",
    "privileges": 2
  },
  {
    "id": "3",
    "name": "Test name 3",
    "privileges": 8
  },
  {
    "id": "4",
    "name": "Test name 4",
    "privileges": 0
  },
  {
    "id": "5",
    "name": "Test name 5",
    "privileges": 15
  }
]

Variable resourceId contains number (id) and receive severally values, for example on first iteration 1, for second 2 and so on.

For resources from example expected output will be:

[1]
[2]
[8]
[]
[1,2,4,8]

You can use reduce if both resource object id and privileges do not exist do not check further just return what you already accrued.

Only if both are present then check the CRUD operations.

const result = resources.reduce((output) => {
  if (resources[i].id !== resourceId && !resources[i].privileges) {
    return output;
  }
  if (CREATE) {
    output.push(CREATE);
  }
  if (READ) {
    output.push(READ);
  }

  if (UPDATE) {
    output.push(UPDATE);
  }

  if (DELETE) {
    output.push(DELETE);
  }
  return output;
}, [])

Create an empty array resultArr . Iterate through resources via forEach and append options to it. Return the array at the end of the function

const generateUsingMap = (resources, resourceId) => {
const resultArr = [];
resources.forEach((resource) => {
    if (resource.id === resourceId && resource.privileges) {
         if (CREATE) {
            resultArr.push(CREATE);
        }

        else if (READ) {
            resultArr.push(READ);
        }

        else if (UPDATE) {
            resultArr.push(UPDATE);
        }

        else if (DELETE) {
            resultArr.push(DELETE);
        }
    }
  });
 return resultArr;
};
const generateUsingMap = (resources, resourceId) => {
    return resources.filter(resource => resource.id === resourceId)
        .map((resource) => {
            if (CREATE & resource.privileges) {
                return CREATE;
            }

            if (READ & resource.privileges) {
                return READ;
            }

            if (UPDATE & resource.privileges) {
                return UPDATE;
            }

            if (UPDATE & resource.privileges) {
                return UPDATE;
            }
        });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM