Change javascript object names

Asked

Viewed 736 times

-2

I have the following object:

{
    "Search Engines": [
        {
            "conta": "Search Engines",
            "hits": 5,
            "bytes": 50189
        },
        {
            "conta": "Search Engines",
            "hits": 1,
            "bytes": 7601
        },
        {
            "conta": "Search Engines",
            "hits": 6,
            "bytes": 613036
        },
        {
            "conta": "Search Engines",
            "hits": 1,
            "bytes": 2858
        }
    ],
    "Content Server": [
        {
            "conta": "Content Server",
            "hits": 1,
            "bytes": 17308
        },
        {
            "conta": "Content Server",
            "hits": 1,
            "bytes": 47412
        },
        {
            "conta": "Content Server",
            "hits": 1,
            "bytes": 24210
        }
    ],
    "Business": [
        {
            "conta": "Business",
            "hits": 1,
            "bytes": 2847
        }
    ],
    "Internet Services": [
        {
            "conta": "Internet Services",
            "hits": 1,
            "bytes": 3690
        }
    ]
}

How can I change the name of all objects ("Search Engines", "Content Server", "Business", "Internet"...) to "test"? I need them to have equal names

2 answers

0

You can’t have them all with the same name. You can do "teste1", "teste2", "teste3", and so on, but calling all the properties of an object with the same name will cause you serious problems. That said, to rename the property, just create a new one with the name you want, and then delete the old one. Example:

objeto.teste3 = objeto.Business;
delete objeto.Business;
console.log(objeto.teste3);

Remembering that in this example, "object" is the name of the object in question (it was not placed in the description of the question)

0

Hi, Leticia, all right?

I was wondering if you wanted to change the names that were added to the properties conta or if you wanted to change the name of the object collections.

If you want to change the name of collections, they can no longer be inside the same object, since the name would be duplicated for all properties. An alternative would be to create a new object, define a new collection with a custom name and make a list of the objects you have. It would look something like this:

{
    "nome_personalizado": [
        {
            "conta": "Search Engines",
            "hits": 5,
            "bytes": 50189
        },
        {
            "conta": "Search Engines",
            "hits": 1,
            "bytes": 7601
        },
        ...
        ,
        {
            "conta": "Content Server",
            "hits": 1,
            "bytes": 17308
        },
        ...
        ,
        {
            "conta": "Internet Services",
            "hits": 1,
            "bytes": 3690
        }
    ]
}

On the other hand, if you want to standardize the attribute conta of the different objects of the collections, so I believe a simple foreach modifying this attribute would solve your problem.

I hope I helped. Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.