Change the name of the javascript objects

-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

    
asked by anonymous 10.08.2018 / 14:58

2 answers

0

You can not have all of them with the same name. You can do "test1", "test2", "test3", 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 (not put in the question description)

    
10.08.2018 / 15:11
0

Hi, Leticia, how are you?

I wondered 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 the collections, they will no longer be in the same object, since the name would be duplicated for all properties. An alternative would be to create a new object, define a new custom named collection, and make a list of the objects that 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 conta attribute of the different objects in the collections, then I believe that a simple foreach modifying this attribute would solve your problem.

I hope I have helped. Hugs.

    
10.08.2018 / 15:17