Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add an element to the beginning when building a map()

enum StatusEnum {
  accepted = "AC",
  rejected = "RJ",
}
const select = (Object.keys(StatusEnum) as Array<keyof typeof StatusEnum>).map((x) => ({
  value: x,
  name: x + "_random",
}))
/**
 * Console.log(select)
 * [
 * 0: { value: "accepted" , name: "accepted_random" }
 * 1: { value: "rejected" , name: "rejected_random" }
 * ]
 */

How to beautifully add an object to the beginning of an array { value: "all" , name: "all_random" } ?

/**
 * Console.log(select)
 * [
 * 0: { value: "all" , name: "all_random" }
 * 1: { value: "accepted" , name: "accepted_random" }
 * 2: { value: "rejected" , name: "rejected_random" }
 * ]
 */

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Surround it with an array and add the item at the beginning with spread operator for the map function.

const select = [{ value: "all" , name: "all_random" }, ...(Object.keys(StatusEnum) as Array<keyof typeof StatusEnum>).map((x) => ({
  value: x,
  name: x + "_random",
}))]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading