Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
change json attribute na,e before save using javascript

this is the sample json format in my application.

my arr =    [
    {
        "id": 12,
        "creationTimestamp": "2021-09-15 15:01:11",
        "groupName": "one",
        "$$hashKey": "object:15"
    },
    {
        "id": 13,
        "creationTimestamp": "2021-09-15 15:01:14",
        "groupName": "two",
        "$$hashKey": "object:16"
    },
    {
        "id": 14,
        "creationTimestamp": "2021-09-15 15:01:19",
        "groupName": "three",
        "$$hashKey": "object:17"
    }
]

according to my requirement i need to change id --> groupId. After i need to assign it for the new array

 my array2       [
        {
            "groupId": 12,
            "creationTimestamp": "2021-09-15 15:01:11",
            "groupName": "one",
            "$$hashKey": "object:15"
        },
        {
            "groupId": 13,
            "creationTimestamp": "2021-09-15 15:01:14",
            "groupName": "two",
            "$$hashKey": "object:16"
        },
        {
            "groupId": 14,
            "creationTimestamp": "2021-09-15 15:01:19",
            "groupName": "three",
            "$$hashKey": "object:17"
        }
    ]

how i do this modification using java script

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can easily achieve the result using map

const my_arr = [
  {
    id: 12,
    creationTimestamp: "2021-09-15 15:01:11",
    groupName: "one",
    $$hashKey: "object:15",
  },
  {
    id: 13,
    creationTimestamp: "2021-09-15 15:01:14",
    groupName: "two",
    $$hashKey: "object:16",
  },
  {
    id: 14,
    creationTimestamp: "2021-09-15 15:01:19",
    groupName: "three",
    $$hashKey: "object:17",
  },
];

const result = my_arr.map(({ id, ...rest }) => ({ groupId: id, ...rest }));
console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

To do this you can use the Object and Array methods in js .. See Below

  let myArray =    [
        {
            "id": 12,
            "creationTimestamp": "2021-09-15 15:01:11",
            "groupName": "one",
            "$$hashKey": "object:15"
        },
        {
            "id": 13,
            "creationTimestamp": "2021-09-15 15:01:14",
            "groupName": "two",
            "$$hashKey": "object:16"
        },
        {
            "id": 14,
            "creationTimestamp": "2021-09-15 15:01:19",
            "groupName": "three",
            "$$hashKey": "object:17"
        }
    ]

    let newArray = [];

    myArray.forEach(obj =>{
        Object.assign(obj , { groupId : obj.id}  );
        delete obj.id;
        newArray.push(obj);
    });

    console.log(newArray);

References : Object.assign and Array.push

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda