How to display key value pairs from nested object - reactjs

I have this structure of data (its more nested, but I give you example of it)
{
status: {
observedGeneration: 2,
replicas: 1,
updatedReplicas: 1,
readyReplicas: 1,
availableReplicas: 1,
conditions: [
{
type: "Progressing",
status: "True",
lastUpdateTime: "2022-02-25T09:28:17Z",
lastTransitionTime: "2022-02-25T09:19:35Z",
},
{
type: "Available",
status: "True",
lastUpdateTime: "2022-09-06T02:03:09Z",
lastTransitionTime: "2022-09-06T02:03:09Z",
}
]
}
settings: {
observedGeneration: 2,
replicas: 1,
updatedReplicas: 1,
readyReplicas: 1,
availableReplicas: 1,
conditions: [
{
type: "Progressing",
status: "True",
reason: "NewReplicaSetAvailable",
message: "ReplicaSet \"label-studio-546655c5cc\" has successfully progressed.
},
{
type: "Available",
status: "True",
reason: "MinimumReplicasAvailable",
message: "Deployment has minimum availability."
}
]
}
}
How I can iterate over this to display in React structure like key value pairs. For example:
This output is only example, but what I want to display is
`key`: `value`
`key`: `value'
`key`:
`key`:`value`
name: Mathew
seetting:
label: 1
composie: yes
additionalSetting:
performance: no
ready: onlyData
version: 123
mountPath: replices
I just was thinking about recursion function, but after many hours of struggling I think I need a little bit of help :)
Thank you so much for any solutions :)

As suggested in comment you can use a tree structure with primitive values as leaf. See this codesandbox for an example of such structure.

Related

MongoDb- get length of array in updateMany (Node.js)

I am trying to do an update many.
The problem that I am encountering is getting the length of an array and seeing if it is less than or equal to another number being passed in from the params.
This is what I have done so far. But I feel this is a bad way of doing it. if anyone has a better way of doing it, please give your input. trying to improve :)
const updateStatus = (materialId, attempts) => {
return db.collection.updateMany(
{
_id,
[`data.${attempts}`]: { $exists: true },
status: { $in: ['outstanding', 'overdue'] },
},
{ status: 'restricted' }
)
}
this is the data I am trying to update many on
[{
_id: 1,
status: 'outstanding',
data:[],
},
{
_id: 2,
status: 'overdue',
data:[{passed: true},{passed: false}],
}
{
_id: 3,
status: 'outstanding',
data:[{passed: true},{passed: false}, {passed: false}],
}
]

How can I display json data in 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.

retrieve all data by size of array in spring mongo template

Suppose I have these:
{id: 1, name: name1, tags: [{id: 1, name: tag1}]},
{id: 2, name: name2, tags: []},
{id: 3, name: name3, tags: [{id: 3, name: tag3}, {id:33, name: tag33}]},
{id: 4, name: name4}
Then execute a query and I want this:
{id: 1, name: name1, tags: [{id: 1, name: tag1}]},
{id: 3, name: name3, tags: [{id: 3, name: tag3}, {id:33, name: tag33}]}
Getting documents that have "tags" array and its size is larger than 0. But don't know how to create my criteria.
I tried this but throws an error saying that size() has to take an argument of int...
where(tags).size().gt(0)
Anyone knows the correct one?
'$size' operator doesn't accept the range parameters. You could use positional value existence to decide the size. as follows
db.collection.find({
"tags.0": {
$exists: true
}
})
In Spring, You could try as follows,
mongoTemplate.find(Query.query(Criteria.where("tags.0").exists(true)));
You can use $not operator:
where("tags").not().size(0).andOperator(where("tags").exists(true));
db.collection.find({
"tags": {
"$not": {
"$size": 0
}
},
"$and": [
{
"tags": {
"$exists": true
}
}
]
})
Alternative: You cannot create this with Spring-data-mongo framework:
db.collection.find({
tags: {
$exists: true,
$not: {
$size: 0
}
}
})

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.

Update embedded mongoose document in array

Lets say that I have the following document in the books collection:
{
_id:0 ,
item: "TBD",
stock: 0,
info: { publisher: "1111", pages: 430 },
tags: [ "technology", "computer" ],
ratings: [ { _id:id1, by: "ijk", rating: 4 }, {_id:id2 by: "lmn", rating: 5 } ],
reorder: false
}
I would like to update the value of ratings[k].rating and all I know is the id of the collection and the _id of the objects existing in the array ratings.
The tutorial of mongoDB has the following example that uses the position of the object inside the array but I suppose that if the update can only be done by knowing the position, this means that I firstly have to find the position and then proceed with the update? Can I do the update with only one call and if so how I can do that?
db.books.update(
{ _id: 1 },
{
$inc: { stock: 5 },
$set: {
item: "ABC123",
"info.publisher": "2222",
tags: [ "software" ],
"ratings.1": { by: "xyz", rating: 3 }
}
}
)
Sorry for late answer; I think this is what you want to do with mongoose.
Books.findOneAndUpdate({
_id: 1,
'ratings._id': id1
},
{
$set: {
'ratings.$.rating' : 3
}
}, function(err, book){
// Response
});
Positional operator may help you:
db.books.update(
// find book by `book_id` with `rating_id` specified
{ "_id": book_id, "ratings._id": rating_id },
// set new `value` for that rating
{ $set: { 'ratings.$.rating': value }}
);
$ will save position of matched document.

Resources