I have an array that I need to bind in a specific way:
const newData = json.map((c) => {
return {
"User Name": c.userName,
Email: c.email,
Groups: c.newArrGroups.map(a => a.name).toString(),
Version: c.clientVersion
};
});
The data stored in the variable json
is passed from a function. Now, I need to check if the array has certain properties. I found a method to do this:
json.hasOwnProperty("userName")
I have two things to verify now. First - I need to check if a specific property exists in the array and remove it if it doesn't; Second - There could be arrays inside an array object. Is there a way to modify the code above to handle this scenario as well?
const newData = json.map((c) => {
return {
if(json.hasOwnProperty("userName")) {
"User Name": c.userName, // Bind the property if it exists
}
Email: c.email,
if(json.newArrGroups.hasOwnProperty("name")) {
Groups: c.newArrGroups.map(a => a.name).toString(), // Check for nested arrays
}
Version: c.clientVersion
};
});