How can I display json data in reactjs - reactjs

My JSON data looks like this:
safarisDetails: {
loading: false,
safaridetails: {
safari: [
{
id: 1,
safari_name: '3 DAYS GORILLA TREKKING TRIP',
days: 3,
days_category: '<4',
budget: '900',
title: '3 DAYS GORILLA TREKKING TOUR',
text: 'Test.',
travelertype: '',
date_created: '2021-10-08T15:22:31.733347Z'
}
],
highlight: [
[
{
id: 1,
highlight: 'See the famous mountain gorillas',
safaris: 1
},
{
id: 2,
highlight: 'Get to know the Batwa community',
safaris: 1
}
]
]
I want to render objects in my component such as the safari_name and loop trough the highlights. However, when I use {safarisdetails.safari.safari_name} i get an error message stating: 'safari_name' is not defined.

There are a couple of problems based on the same you provided...
safarisdetails is repeated twice in the JSON, but not in your check
safari is an array in the JSON, but accessed like an object in your check
For example, to get the first name, you'd write something like:
safarisdetails.safarisdetails.safari[0].safari_name
Also, is this JSON even valid? Maybe you posted a partial snippet, but the JSON has to have {} around the whole thing.

Related

How to add and update object into array of objects in PostgreSql?

I am learning postgresql. I have this type of data that i want to add in my database.
{
id: (Math.random().toString(36)+'00000000000000000').slice(2, 7),
title: "This is a sample todo",
status: 'pending',
created_at: new Date(),
subTask: [
{
text: "This is a sampe subtask",
isDone: false,
},
{
text: "This is a sample subtask",
isDone: false,
}
]
}
My question is how can i update and add objects in subtasks array using postgresql queries.
I have so far tried this one
update
todos
set
subtasks = '[{"title": "Task No 2", "status": "pending"}]'
where
id = 6;
But it is only replacing the existing object present not adding it. I have also searched on the google and elsewhere but so far don't find anything.
Thank you

how to loop my react data, its not showing my data

I makes crud with react but i have trouble when try to displaying from data table. please help me
this my code
my conn to api
this my looping data
looping code
this the console log
console log
I saw that you make an array which contains a lot of arrays. Is this your expectation?
rowsData = [
[{ DocumentID: 1, DocumentName: 2, status: 3, navigation: 4 }],
[{ DocumentID: 1, DocumentName: 2, status: 3, navigation: 4 }]
]

How can I use Papa Parse or Angular to merge objects based on a certain attribute?

I'm using Papa Parse to convert a CSV file with name, date and result to JSON objects, but I want objects based on the names instead of the rows of the CSV file. If this isn't possible using Papa Parse, is there a method within Angular (or an Angular package) that I can use to do this?
I get the following output:
[
{
name: 'foo',
date: '11-10-2016',
result: '10'
},
{
name: 'foo',
date: '12-10-2016',
result: '8'
},
{
name: 'bar',
date: '11-10-2016',
result: '1'
},
{
name: 'bar',
date: '15-10-2016',
result: '4'
}
]
But I want this output:
[
{
name: 'foo',
results:
[
{
date: '11-10-2016',
result: '10'
},
{
date: '12-10-2016',
result: '8'
}
]
},
{
name: 'bar',
results:
[
{
date: '11-10-2016',
result: '1'
},
{
date: '15-10-2016',
result: '4'
}
]
}
]
I found an answer how to do it with plain javascript here, but if there's a function to do it within Papa Parse or Angular I would prefer to use that.
Papaparse is designed to only parse data but not manipulate it, so there is (and I'm wondering that it will never be) a function for aggregating the parsed data.
Having said that, I think that using the plain javascript function after parsing the data will be enough to achieve what you want.

how to merge similar values in normalizr function?

I have unusual response from server like this
[
{
id: 1,
name: "Alexandr",
children: [
{
id: 2,
name: "Stephan"
},
{
id: 3,
name: "Nick"
}
]
},
{
id: 4,
name: "David",
children: [
{
id: 3,
name: "Nick"
},
{
id: 6,
name: "Paul"
}
]
}
]
i would like to normalize this response to receive a diction with all people. So, i use normalizr go flat this
const people= new Schema('people');
people.define({
Children: arrayOf(people),
NotOwnChildren: arrayOf(people)
});
let normalized = normalize(response.data, arrayOf(people));
but doing like this i get an error
"When merging two people, found unequal data in their "Children" values. Using the earlier value."
How can i adjust normalizr to merge people with same id (update entities with newest data)?
It looks like you're getting two people that have differing values for one of their keys (I'm assuming your example input is truncated).
For Normalizr#2:
You can use a custom mergeIntoEntity function to resolve the issue manually.
For Normalizr#>=3.0.0, you'll need use mergeStrategy.

Filter array elements by nested array of objects

First, let me say that this question is slightly different to others I've seen regarding nested arrays in mongo.
Most of them ask how to get a specific element in a nested array. I want to get all elements of an array that has itself another array containing a given value.
I have documents, that look like this:
{
_id: something,
value: something,
items: [{
name: someItem,
price: somePrice,
stores:[
{name: storeName, url: someUrl },
{name: anotherStoreName, url: someOtherUrl}
]
},
{
name: someOtherItem,
price: someOtherPrice,
stores:[
{name: storeName, url: someUrl },
{name: yetAnotherStoreName, url: someOtherUrl}
]
}]
}
What I want is to get only the items elements that have a given store in the stores array.
This is, if I ask storeName, I should get both items in the example. But if I ask for anotherStoreName, I should only get the first element of the array items.
Searching for similar questions and trying the solutions I can only get the first matching element of items, but I want all the matching elements.
I'd appreciate any help.
You should use mongo aggregation to get result in following way.
First use $unwind to separate array of items and then add match condition.
db.collection.aggregate([{
$unwind: "$items"
}, {
$match: {
"items.stores.name": "storeName"
}
}, {
$project: {
_id: 0,
items: "$items" //you can add multiple fields here
}
}]);
You should use the aggregation framework to unwind elements on the items array and then treat them as individual documents.
db.data.aggregate([
{
$unwind: "$items"
},
{
$project: {
_id: 0,
items: "$items"
}
},
{
$match: {
"items.stores.name": "yetAnotherStoreName"
}
}
]);
$project just let you work with the important part of the document.
This works on the mongoshell be carefully when using your driver.
The result.

Resources