0

I have a json like this:

   "Client" : {
        "ClientId" : "eertertwetw",
        "Username" : "c.client",
        "Names" : [
            {
                "Family" : "ClientFamilyName",
                "Given" : [
                    "ClientGivenName"
                ]
            }
        ]
    }

This json is not fixed, so sometimes there are some properties and sometimes not. I need to replace every array inside this Json with the first element of that array. So, for example, in this case it would be like

   "Client" : {
        "ClientId" : "eertertwetw",
        "Username" : "c.client",
        "Names" :
            {
                "Family" : "ClientFamilyName",
                "Given" : 
                    "ClientGivenName"
            }
        ]
    }

Can anyone help me to find a way to do this with Typescript?

3
  • Problem statement is not clear yet, What you want prefix in every object? Commented Feb 22, 2019 at 18:15
  • @PardeepJain yes, it is clear. He wants to replace every array in that object by first element of that array :) Commented Feb 22, 2019 at 18:36
  • @romanSG is right. I'm searching something to create a new object with that replace. Commented Feb 22, 2019 at 19:24

3 Answers 3

1

Ok, I tried something (haven't tested it against all possible cases) but it seems like it's working.

Stackblitz - check the console for result.

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

1 Comment

Hi, thank you for the reply. I found your answer very good for what I have to do. Thank you very much.
1
let data = { Client: {...} }; // your data

data = data.map(client => {
    if (!Object.hasOwnProperty(client.Names, 'Prefix')) {
        client.Names.Prefix = null;
    }
    return client;
});

Comments

0

You need get the firt element in array in object.

like : Client.Names[0].Given[0].ClientGivenName

Comments

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.