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

182
Visualizações
How To Add A String Inside An Object For The Given Example

Hi iam finding the best way to add a string inside the given object.Any help would be appreciated

my String is 'created'

Down Below Is My Data

{
    "id": "222",
    "list": [
        {
            "name": "Tony",
      
        }
    ], 

iam trying to insert 'created' in the data like this

 {
    "id": "222",
    "list": [
        {
            "name": "Tony",
            "type":"created"
      
        }
    ]
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The string you've provided looks a lot like JSON data. You can convert a JSON string to an actual javascript object by using the JSON.parse(string) method.

With this object we then can query it's list property - which in your case is an array of objects - and add a new property type to each of the arrays elements. The final step is converting the object back to a JSON string using the JSON.stringify(object) method.

Here's an example:

let str = `{
    "id": "222",
    "list": [
        {
            "name": "Tony" 
        }
    ]
  }`;
let data = JSON.parse(str);
data.list.forEach(element => {
  element.type = "created";
});
str = JSON.stringify(data);
console.log(str);

about 4 years ago · Juan Pablo Isaza Relatório

0

const myObj = {
    "id": "222",
    "list": [
        {
            "name": "Tony",
  
        }
    ], 
};
myObj.list[0].type = "created";

This is the way you can do this. But you'd better use secified index of list items;

const index = 0; // Or any other way to get this

myObj.list[index].type = "created";
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