Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!