You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
304 lines
5.7 KiB
304 lines
5.7 KiB
# Day 16 - Json
|
|
|
|
|
|
## [Exercise:Solutions](#exercise-solutions)
|
|
|
|
- ### [Exercise:Level 1](#exercises-level-1)
|
|
- ### [Exercise:Level 2](#exercises-level-2)
|
|
- ### [Exercise:Level 3](#exercises-level-3) <hr>
|
|
|
|
|
|
#### [Home](../README.md) | [<< Day 15](./15_day_classes.md) | [Day 17 >>](./17_day_wep%20storege.md)
|
|
|
|
## Exercise Solutions
|
|
|
|
```js
|
|
const skills = ['HTML', 'CSS', 'JS', 'React','Node', 'Python']
|
|
let age = 250;
|
|
let isMarried = true
|
|
const student = {
|
|
firstName:'Asabeneh',
|
|
lastName:'Yetayehe',
|
|
age:250,
|
|
isMarried:true,
|
|
skills:['HTML', 'CSS', 'JS', 'React','Node', 'Python', ]
|
|
}
|
|
const txt = `{
|
|
"Alex": {
|
|
"email": "alex@alex.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript"
|
|
],
|
|
"age": 20,
|
|
"isLoggedIn": false,
|
|
"points": 30
|
|
},
|
|
"Asab": {
|
|
"email": "asab@asab.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript",
|
|
"Redux",
|
|
"MongoDB",
|
|
"Express",
|
|
"React",
|
|
"Node"
|
|
],
|
|
"age": 25,
|
|
"isLoggedIn": false,
|
|
"points": 50
|
|
},
|
|
"Brook": {
|
|
"email": "daniel@daniel.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript",
|
|
"React",
|
|
"Redux"
|
|
],
|
|
"age": 30,
|
|
"isLoggedIn": true,
|
|
"points": 50
|
|
},
|
|
"Daniel": {
|
|
"email": "daniel@alex.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript",
|
|
"Python"
|
|
],
|
|
"age": 20,
|
|
"isLoggedIn": false,
|
|
"points": 40
|
|
},
|
|
"John": {
|
|
"email": "john@john.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript",
|
|
"React",
|
|
"Redux",
|
|
"Node.js"
|
|
],
|
|
"age": 20,
|
|
"isLoggedIn": true,
|
|
"points": 50
|
|
},
|
|
"Thomas": {
|
|
"email": "thomas@thomas.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript",
|
|
"React"
|
|
],
|
|
"age": 20,
|
|
"isLoggedIn": false,
|
|
"points": 40
|
|
},
|
|
"Paul": {
|
|
"email": "paul@paul.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript",
|
|
"MongoDB",
|
|
"Express",
|
|
"React",
|
|
"Node"
|
|
],
|
|
"age": 20,
|
|
"isLoggedIn": false,
|
|
"points": 40
|
|
}
|
|
}
|
|
```
|
|
### Exercises Level 1
|
|
|
|
1. Change skills array to JSON using JSON.stringify()
|
|
```js
|
|
//app.js
|
|
|
|
const skills = ['HTML', 'CSS', 'JS', 'React','Node', 'Python']
|
|
|
|
console.log(JSON.stringify(skills))
|
|
```
|
|
1. Stringify the age variable
|
|
```js
|
|
//app.js
|
|
|
|
let age = 250;
|
|
console.log(JSON.stringify(age))
|
|
```
|
|
1. Stringify the isMarried variable
|
|
```js
|
|
//app.js
|
|
|
|
let isMarried = true
|
|
console.log(JSON.stringify(isMarried))
|
|
```
|
|
1. Stringify the student object
|
|
```js
|
|
//app.js
|
|
|
|
const student = {
|
|
firstName:'Asabeneh',
|
|
lastName:'Yetayehe',
|
|
age:250,
|
|
isMarried:true,
|
|
skills:['HTML', 'CSS', 'JS', 'React','Node', 'Python', ]
|
|
}
|
|
|
|
console.log(JSON.stringify(student))
|
|
```
|
|
|
|
### Exercises Level 2
|
|
|
|
1. Stringify the students object with only firstName, lastName and skills properties
|
|
```js
|
|
//app.js
|
|
|
|
const student = {
|
|
firstName:'Asabeneh',
|
|
lastName:'Yetayehe',
|
|
age:250,
|
|
isMarried:true,
|
|
skills:['HTML', 'CSS', 'JS', 'React','Node', 'Python', ]
|
|
};
|
|
|
|
const studentString = JSON.stringify({
|
|
firstName: student.firstName,
|
|
lastName: student.lastName,
|
|
skills: student.skills
|
|
});
|
|
|
|
console.log(studentString);
|
|
|
|
```
|
|
|
|
### Exercises Level 3
|
|
|
|
1. Parse the *txt* JSON to object.
|
|
```js
|
|
//app.js
|
|
|
|
const obj = JSON.parse(txt);
|
|
console.log(obj);
|
|
|
|
```
|
|
2. Find the user who has many skills from the variable stored in *txt*.
|
|
```js
|
|
//app.js
|
|
|
|
const txt = {
|
|
"Alex": {
|
|
"email": "alex@alex.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript"
|
|
],
|
|
"age": 20,
|
|
"isLoggedIn": false,
|
|
"points": 30
|
|
},
|
|
"Asab": {
|
|
"email": "asab@asab.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript",
|
|
"Redux",
|
|
"MongoDB",
|
|
"Express",
|
|
"React",
|
|
"Node"
|
|
],
|
|
"age": 25,
|
|
"isLoggedIn": false,
|
|
"points": 50
|
|
},
|
|
"Brook": {
|
|
"email": "daniel@daniel.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript",
|
|
"React",
|
|
"Redux"
|
|
],
|
|
"age": 30,
|
|
"isLoggedIn": true,
|
|
"points": 50
|
|
},
|
|
"Daniel": {
|
|
"email": "daniel@alex.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript",
|
|
"Python"
|
|
],
|
|
"age": 20,
|
|
"isLoggedIn": false,
|
|
"points": 40
|
|
},
|
|
"John": {
|
|
"email": "john@john.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript",
|
|
"React",
|
|
"Redux",
|
|
"Node.js"
|
|
],
|
|
"age": 20,
|
|
"isLoggedIn": true,
|
|
"points": 50
|
|
},
|
|
"Thomas": {
|
|
"email": "thomas@thomas.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript",
|
|
"React"
|
|
],
|
|
"age": 20,
|
|
"isLoggedIn": false,
|
|
"points": 40
|
|
},
|
|
"Paul": {
|
|
"email": "paul@paul.com",
|
|
"skills": [
|
|
"HTML",
|
|
"CSS",
|
|
"JavaScript",
|
|
"MongoDB",
|
|
"Express",
|
|
"React",
|
|
"Node"
|
|
],
|
|
"age": 20,
|
|
"isLoggedIn": false,
|
|
"points": 40
|
|
}
|
|
};
|
|
|
|
const userWithMostSkills = Object.keys(txt).reduce((a, b) => txt[a].skills.length > txt[b].skills.length ? a : b);
|
|
|
|
console.log(`The user with the most skills is ${userWithMostSkills}.`);
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### [Home](../README.md) | [<< Day 15](./15_day_classes.md) | [Day 17 >>](./17_day_wep%20storege.md) |