Friday , December 6 2019
Home / Uncategorized / How to make CRUD operations in JSON (part 2)

How to make CRUD operations in JSON (part 2)




Before starting with the CRUD operations, please inform yourself about:


CRUD operations are things that will make you a programmer.

With JSON you can use an intelligent data structure to avoid the search for brute force with for loops. Using the "character" name as "Primary Key".

The hidden hidden installation code JavaScript enters this preamble area

//Data
var got = {
"John Snow": {"name": "Kit Harington", "gender": "Male", "house": "Starks"},
"Daenerys": {"name": "Emilia Clarke", "gender": "Female", "house": "Targaryens"},
"Tyrion": {"name": "Peter Dinklage", "gender": "Male", "house": "Lanisters"}
}

Add character (CReate):

The hidden hidden installation code JavaScript enters this preamble area
var got = {
"John Snow": {"name": "Kit Harington", "gender": "Male", "house": "Starks"},
"Daenerys": {"name": "Emilia Clarke", "gender": "Female", "house": "Targaryens"},
"Tyrion": {"name": "Peter Dinklage", "gender": "Male", "house": "Lanisters"}
}

// Click [RUN] button to see the result
var characterObject = {
"name": "Bella Ramsey",
"female gender",
"house": "Mormonts"
};
var character = "Lyanna";
had[character] = characterObject;
console.log (GOT)

Find character (Read):

The hidden hidden installation code JavaScript enters this preamble area
var got = {
"John Snow": {"name": "Kit Harington", "gender": "Male", "house": "Starks"},
"Daenerys": {"name": "Emilia Clarke", "gender": "Female", "house": "Targaryens"},
"Tyrion": {"name": "Peter Dinklage", "gender": "Male", "house": "Lanisters"}
}

// Click [RUN] button to see the result
var character = "Tyrion"; // Or anything else
var result = got[character];
console.log (result)
console.log (result.name)
console.log (result.gender)
console.log (result.house)

Update character (Update):

The hidden hidden installation code JavaScript enters this preamble area
var got = {
"John Snow": {"name": "Kit Harington", "gender": "Male", "house": "Starks"},
"Daenerys": {"name": "Emilia Clarke", "gender": "Female", "house": "Targaryens"},
"Tyrion": {"name": "Peter Dinklage", "gender": "Male", "house": "Lanisters"}
}

// Click [RUN] button to see the result
var character = "John Snow";
had[character].house = "Targaryens"
var result = got[character];
console.log (result)

Remove character (Delete):

The hidden hidden installation code JavaScript enters this preamble area
var got = {
"John Snow": {"name": "Kit Harington", "gender": "Male", "house": "Starks"},
"Daenerys": {"name": "Emilia Clarke", "gender": "Female", "house": "Targaryens"},
"Tyrion": {"name": "Peter Dinklage", "gender": "Male", "house": "Lanisters"}
}

// Click [RUN] button to see the result
var character = "Tyrion";
eliminated obtained[character];
console.log (GOT)

Maybe it's nice, but you have to take care of the array positions on the CRUD operations.

The hidden hidden installation code JavaScript enters this preamble area

//Data
var got = {
"index": {
"John Snow": 0, "Daenerys": 1, "Tyrion": 2
},
"data": [
{"name": "Kit Harington", "gender": "Male", "house": "Starks"},
{"name": "Emilia Clarke", "gender": "Female", "house": "Targaryens"},
{"name": "Peter Dinklage", "gender": "Male", "house": "Lanisters"}
] }

var character = "Daenerys";
index var = got.index[character];
var data = got.data[index];
console.log (data);


It's all people!
Happy coding

dev.to is the place where software developers stay in the circuit and avoid career stagnation.
Registering (for free!) Is the first step.


Source link

Leave a Reply

Your email address will not be published.