Check Array Contains A Property and Bind It To A New Array

Discussão em 'Angular' iniciado por user8512043, Dezembro 15, 2024.

  1. user8512043

    user8512043 Guest

    I've an array that I am binding as follows:

    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 above json data is passed from a function, now I've a requirement to check if the array has specific properties. So I found something that can check the properties as follows:

    json.hasOwnProperty("userName")


    I've two things to verify now. One - Have to check if specific property is in the array and if the property doesn't exist, then remove it from there; Second - There could be arrays inside an array object. Is there any solution with the above that I can check something as follows:

    const newdata = json.map((c) => {
    return {

    if(json.hasOwnProperty("userName")) {
    "User Name": c.userName, //To check if property exists, then bind it here
    }

    Email: c.email,

    if(json.newArrGroups.hasOwnProperty("name")) {
    Groups: c.newArrGroups.map(a => a.name).toString(), //This checking is required as well as there would be arrays inside an array object
    }

    Version: c.clientVersion
    };
    });

    Continue reading...

Compartilhe esta Página