Correct structure of related data in the API method - reactjs

Good afternoon!
I'm developing an API for a web service. I need to render the form with default data downloaded from server.
The method 'small-form' returns dictionaries of countries and cities.
Every city has an id of the country where it is located. If we need to know all cities in the country - it is necessary to iterate through all the cities.
There are two options to solve this problem:
1.Add an list of id's with all cities related to the countries.
2.Create a separate dictionary, where the keys are id's of the countries, and the values ​​are the lists of cities id's.
I can not understand how will it be more correct:
{
"Countries": {
"country id": {
"id": "country id",
"name": "country name",
"citys": ["city id", "city id" ...]
},
},
"Cities": {
"city id": {
"id": "id of the city",
"name": "city name",
"country__id": "country id",
}
}
}
or so:
{
"Countries": {
"country id": {
"id": "country id",
"name": "country name",
},
},
"Cities": {
"city id": {
"id": "id of the city",
"name": "city name",
"country_id": "country id",
}
},
"countries_cities":{
"country id":["id city", "city"...]
}
}

Related

How to insert an array into an object of arrays in SpringBoot using MongoDB?

Hi I'm a beginner and I have very little clue about trying to insert an array into an already existing one.
I have the JSON structure here, and I'm trying to insert another array. I tried looking for it online but I couldn't find enough resources, for inserting data into a nested object.
{
"mainbranch": "Assets",
"childOfMainBranch": [
{
"mainbranch": "External Accounts",
"childOfMainBranch": [
{
"mainbranch": "Cash and Cash Balances",
"childOfMainBranch": [
{
"mainbranch": "Cash Reserve",
"count": 1010131,
"createdOn": "30-08-2020",
"currency": "EUR",
"createdBy": "Company A"
},
{
"mainbranch": "Values Undergoing Collection",
"count": 1012131,
"createdOn": "31-09-2021",
"currency": "EUR",
"createdBy": "Company B"
},
{
"mainbranch": "Bank Account Number",
"count": 1022131,
"createdOn": "14-05-2019",
"currency": "EUR",
"createdBy": "Company A"
}
]
}
]
}
],
"_class": "com.tree.model.Tree"
}

How can l only display objects by category from the json response?

I'm getting the following JSON response from a PHP backend, and l want to display in a slider using the category field for example first slider will contain only objects with the category "Basic Training" and another slider will have objects containing the category "Policy Training in angular 10, component.html, see the response and check the categories field.
{
"status": 0,
"course": [
{
"id": 16,
"name": "First Training",
"category": "Basic Training",
"description": "first training description",
"preview_image": "/preview/first.png",
"created_at": "2021-08-15 15:40:06"
},
{
"id": 17,
"name": "Second Training",
"category": "Basic Training",
"description": "second training description ",
"preview_image": "/preview/second.png",
"created_at": "2021-08-15 15:47:18"
},
{
"id": 19,
"name": "Security Policies",
"category": "Policy Training",
"description": "This course is about security",
"preview_image": "/preview/cyberprofile.jpeg",
"created_at": "2021-09-15 00:29:03"
},
{
"id": 20,
"name": "First Corrective",
"category": "Corrective Training",
"description": "This course is about training",
"preview_image": "/preview/kids-learning.jpeg",
"created_at": "2021-09-15 00:31:46"
}
],
"message": "courses retrieved successfully"
}
You can group your element by category and display your result. If you like this method here is sample code.
let json_val = {
"status": 0,
"course": [
{
"id": 16,
"name": "First Training",
"category": "Basic Training",
"description": "first training description",
"preview_image": "/preview/first.png",
"created_at": "2021-08-15 15:40:06"
},
{
"id": 17,
"name": "Second Training",
"category": "Basic Training",
"description": "second training description ",
"preview_image": "/preview/second.png",
"created_at": "2021-08-15 15:47:18"
},
{
"id": 19,
"name": "Security Policies",
"category": "Policy Training",
"description": "This course is about security",
"preview_image": "/preview/cyberprofile.jpeg",
"created_at": "2021-09-15 00:29:03"
},
{
"id": 20,
"name": "First Corrective",
"category": "Corrective Training",
"description": "This course is about training",
"preview_image": "/preview/kids-learning.jpeg",
"created_at": "2021-09-15 00:31:46"
}
],
"message": "courses retrieved successfully"
}
var groupBy = (arr, key) => {
var initialValue = {};
return arr.reduce((acc, cval) => {
var myAttribute = cval[key].toLowerCase().replace(" ", "_");
acc[myAttribute] = [...(acc[myAttribute] || []), cval]
return acc;
}, initialValue);
};
var res = groupBy(json_val.course, "category");
console.log("group by:", res);
You could just have a method that receives the category you want to filter for and returns the array of items corresponding to that category.
Example 1:
getCoursesByCategory(category: string) {
return this.serverResponse.course.filter(c => c.category === category)
}
And use it like this.getCoursesByCategory('Basic Training')
const data = { /* Your json */ };
const categorizedData = data.course.reduce((r, a) => {
r[a.category] = [...r[a.category] || [], a];
return r;
}, {});
console.log(categorizedData);

React Native Call Api

"bank_account": Object {
"name": "User Name",
"account": "4916111323",
"bank": "City Bank",
},
here i can call
item.bank_account.name
item.bank_account.account
item.bank_account.bank
but here its not working
item.menu.image
"menu": Array [ Object {
"image": "3",
"item_id": "1",
"price": "450"},
],
should be something between menu and image because it object?
item.menu is Array. You need to access it's indexes: item.menu[INDEX].image (index is a zero-based index) e.g. item.menu[0].image

Unable to fetch nested data into PrimeNG table

I am very new to PrimeNG table and I tried to load nested json doc into table, but I am unable to access data of nested array.
I am able to load flatten json in p-table but in nested json unbale to fetch the nested data
EX: { field: "address", header: "Address ", title: "Person Address" }
for this code am getting [object, object] in address column
I also tried with indexing like field address[0].id but not getting
This sample data of json fetching from API
Retailers=[
{
"id":1,
"name":"Preeti Mobiles",
"address":[{
"id": 1,
"addressLine1":"31/A, 2nd cross, 1st floor",
"addressLine2":"Tirumala Towers, Mejestic",
"city":"Bengaluru",
"state":"Karnataka",
"country":"India",
"pincode":500006
},
{
"id": 2,
"addressLine1":"31/A, 2nd floor",
"addressLine2":"Hebbal",
"city":"Bengaluru",
"state":"Karnataka",
"country":"India",
"pincode":500091
}
]
},
{
"id": 2,
"name": "Nanda Electronics",
"address": [{
"id": 1,
"addressLine1": "78/A, 4nd cross, 1st floor",
"addressLine2": "Kishan Empower, RR Nagar",
"city": "Bengaluru",
"state": "Karnataka",
"country": "India",
"pincode": 520006
}
]
},
{
"id": 3,
"name": "Kishan Electronics",
"address": [{
"id": 1,
"addressLine1": "86/A, 1nd cross, 2st floor",
"addressLine2": "Nanda Building, Hebbala",
"city": "Bengaluru",
"state": "Karnataka",
"country": "India",
"pincode": 520036
}
]
}
]
Type script code
ngOnInit() {
this.person();
}
person(){
this.personService.getpersonList().subscribe(data=>{
this.person_details = data;
console.log(this.person_details);
});
this.retailer_list = [
{ field: "name", header: "Name", title: "person Name" },
{ field: "address", header: "Address ", title: "person Address" }
];
}
}

How can i handle this scenario

I am using Backbone.js, unexpectedly i am in the confused position now, because of this scenario..
my test object :
test: {
"links": {
"nameLink": {
"name": "name 1",
"name": "name 2",
"name": "name 3"
},
"ageLink": {
"age": "1",
"age": "2",
"age": "3"
}
},
"data": {
"element": {
"project": "project 1",
"title": "title1",
"brand": "brand1"
},
"element": {
"project": "project 2",
"title": "title2",
"brand": "brand2"
},
"element": {
"project": "project 3",
"title": "title3",
"brand": "brand3"
}
}
}
I do have the josn data from server, it has the project name, links(there is nested links under), time stamps.
how can i split the data, and append appropriate UL elements. ( by name, by age ect.)
On click on the name, age links, how can sort (by name, age) the datas i appended to body tag? - without sorting the links menu?
How can i keep all this updated?
any good suggestion please?

Resources