0

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
  };
});
1
  • Can you provide a demo, maybe stackblitz Commented Dec 15, 2024 at 15:22

1 Answer 1

0

just a basic problem here. I have demonstrated it here.

// Sample JSON data
const json = [
    {
        userName: "JohnDoe",
        email: "[email protected]",
        newArrGroups: [{ name: "Admin" }, { name: "User" }],
        clientVersion: "1.0.0"
    },
    {
        email: "[email protected]",
        newArrGroups: [{ name: "User" }],
        clientVersion: "1.0.1"
    },
    {
        userName: "Alice",
        newArrGroups: [{ name: "Admin" }],
        clientVersion: "1.0.2"
    }
];

// Mapping function
const newdata = json.map((c) => {
    const result = {};
    if (c.hasOwnProperty("userName")) {
        result["User Name"] = c.userName;
    }

    if (c.hasOwnProperty("email")) {
        result.Email = c.email;
    }

    if (c.hasOwnProperty("newArrGroups") && Array.isArray(c.newArrGroups)) {
        result.Groups = c.newArrGroups.map(a => a.name).toString();
    }

    if (c.hasOwnProperty("clientVersion")) {
        result.Version = c.clientVersion;
    }

    return result;
});

// Display the result on the web page
document.getElementById('output').textContent = JSON.stringify(newdata, null, 2);
<h1>Mapped Data</h1>
<div id="output"></div>

Please try it and let me know if it helps! :)

Sign up to request clarification or add additional context in comments.

3 Comments

Looks promising, let me try @PPFromInfy. Thanks a lot.
Please accept and upvote the answer if it helps! :)
Sure, will check in some time :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.