How to display element from an array - reactjs

In the below image there is an array given to me on doing console.log
Array on doing console.log
The name of the array is commentData
if i do commentData.name i get the name
but when i do commentData.replyoncomment.Rname or commentData.replyoncomment[0].Rname
i get undefined even on commentData.replyoncomment
how do i solve this problem
if you need anything else ask me.
Update:
Thats how i turned commentData to an array
const convert = JSON.stringify(comment)
const comments = Object.values(JSON.parse(convert))
const commentData = comments
when i do commentData.name or commentData.commment
it give me the name/comment
but when i do commentData.replyoncomment i get undefined.
see the first image to understand better
on running this i get
{JSON.stringify(commentData)}
[[{"_createdAt":"2022-08-07T14:34:40Z","_id":"dqDIn547oWb3s2Y4OEwhMK","comment":"sdfanj akf js","name":"nkfndm nj","replyoncomment":[]},{"_createdAt":"2022-08-07T03:13:01Z","_id":"SD6gTXwnOmN4hstl5nkSbH","comment":"hello, I'm the admin of the webstie","name":"Mian Mohid Naeem","replyoncomment":[{"Rcomment":"gfdhhh","Rname":"gff","_createdAt":"2022-08-07T10:17:33Z"},{"Rcomment":"print("Hello world")","Rname":"programmer","_createdAt":"2022-08-07T03:13:40Z"}]}]]
anything else you need to see in the code ask me...

You should map the commentData.replyoncomment to, cause it is an array too, and React can't render arrays.
So nest an additional map to the commentData.replyoncomment.
I was willing to help you, if you have any more questions, ask me.

Related

How to add an array to Firebase Firestore Swift

I am trying to update the data in a document in Cloud Firestore with an array however whenever I try to do this I get an error: Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Unsupported type: __SwiftValue (found in field upvotes)'. I would really appreciate it if someone could help me understand what I am doing wrong and how I can fix this. Thanks. Code is below.
Code for updating array-
db.collection("forum").document(data[0].id!).setData(["upvotes": data[0].upvotes!.remove(at: data[0].upvotes!.firstIndex(of: MainView.username ?? "Anonymous")!)])
You must pass an array to Firestore to update an array property. What you're doing is passing an element of an array. The remove(at:) method, when used like this:
let removedElement = someArray.remove(at: someIndex)
will remove the element but return the value of that removed element; it doesn't return the updated array. You must first remove the element and then get the truncated array:
someArray.remove(at: someIndex)
let updated = someArray
Therefore, first remove, then update:
data[0].upvotes!.remove(at: data[0].upvotes!.firstIndex(of: MainView.username ?? "Anonymous")! // first remove
db.collection("forum").document(data[0].id!).setData([
"upvotes": data[0].upvotes!) // then pass
])
However, I'd recommend reformatting the code because it doesn't read very well, IMO, and there is too much force unwrapping for my taste.

How to retrieve specific value of an array from firestore

I am retrieving specific data as an array from firestore, the value is the there on the console.log(), but i cant seems to retrieve the specific data from the array itself,
here's my event.ts
import { Event } from '../../models/event';
invitedEvents: Event[] = [];
this.invitedEvents = invitedEvents;
console.log(invitedEvents, invitedEvents.name);
on console.log()
as you can see the invitedEvents.name return value of undefined, i'm sure you guys know the proper way of retrieving the value of name, send help.
The object is in the array, so you have to access object from array first invitedEvents[0]
console.log(invitedEvents, invitedEvents[0].name);
Here you have to invitedEvents is an array. So if you need to access you can't directly like invitedEvents.name like this, here is the few ways to access.
for(let item of invitedEvents){
console.log("Data",item);
console.log("Specific Name",item.name);
}
This example is used to print multiple values.
If you have only one value.Simply do like this.
console.log("Specific Name Second way", invitedEvents[0].name);
I hope its helps you.
Thanks,
Muthu

How to pass the objects of myArray?

I'm a newbie, I have gone through basics but couldn't figure this one out. I've googled so much but finally I land here.
fontFamily = [[UIFont alloc]init];
fontSize = [[NSMutableArray alloc]initWithObjects:#"10",#"15",#"20",#"25",#"30", nil ];
[text setFont: [[UIFont familyNames] size:<-----
And now, how can I pass the objects to size?
I truly don't know what your looking for. But if you are looking to access the strings you put in the array, you can do so like this:
[fontSize objectAtIndex:2]
You can put that any place you would want to put #"20" since that is what is at index 2.
You should note that you are putting string values in the array not number values.
Edit in response to comment.
You don't really want to just pass the array anywhere what you want to do is something like:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
// if calling for size component
return fontSize.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
// if calling for size component
return [fontSize objectAtIndex:row];
}
If I'm off a little I apologize, I don't use pickerViews much.

how to update global variable in titanium?

i'm having some problem in updating my array which is global by the way.
here is my code:
Ti.App.dinercolor=["#FF5A00","#007EFF","#dccdc0","#C2FF95","#A700FD","#dccdc0","#dccdc0","#5F9EA0","#dccdc0","#dccdc0","#22A000","#DCCDC0","#dccdc0","#FF003C","#dccdc0","#FF003C","#dccdc0","#22A000","#dccdc0","#FFF191"];
thats my global array which i can access data from it from anywhere in the application.
the problem comes when i want to update the array like:
for(var q=0; q<Ti.App.dinercolor.length; q++){Ti.App.dinercolor[q] = '#dccdc0';}
so, the array i was expecting after the operation thats done is something like this:
Ti.App.dinercolor=["#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0"];
but somehow i'm getting the same array with out updating,
Ti.App.dinercolor=["#FF5A00","#007EFF","#dccdc0","#C2FF95","#A700FD","#dccdc0","#dccdc0","#5F9EA0","#dccdc0","#dccdc0","#22A000","#DCCDC0","#dccdc0","#FF003C","#dccdc0","#FF003C","#dccdc0","#22A000","#dccdc0","#FFF191"];
please help me out, i have no idea what i'm doing wrong here,
Thank you,,
Your code is correct, but you shouldn't extend the Ti object as unexpected things like this will happen. Create your own object and it will work.
myObj = {};
myObj.dinercolor = [];
And so on.
It is recommended you keep your app in a single context so you will be able to access the object from anywhere. Check out the forging titanium video series for some best practices.
I agree with Jeff, however if you want the above approach to work you will need to update the whole array, you cannot just update elements.
So read the array out into a new variable, update the specific elements and then set the property again
In App.js:
Ti.App.my_variable = 0;
In some_other_page.js:
Ti.App.my_variable = 101;
In yet_another_page.js:
alert( Ti.App.my_variable );
This will alert 101 !!

Accessing variable from other class returns null

I did a separate levelData class to be able to flexibly add levels. I was happy with it until my supervisor ordered me to convert my levelData into XML. I did an XML version of the levelData's data (question, answers, correct answer...). I used the old class and converted it so that it fetches the XML.
All seems well, I did traces of my answers array and it printed nicely...
But the headache started when I tried this.
// This code appears in a different class with
// currentLvl:LevelData initialized in the constructor.
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
// BTW, I can't make a for loop to do the same function as above. So wierd.
I tried to run it. it returned:
TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set text()
at QuestionPane/setQuiz()
at QuestionPane/setQuestion()
at QuestionPane()
at LearningModule()
Where did I go wrong? I tried making a custom get function for it, only to get the same error. Thanks in advance. If I need to post more of the code, I will gladly do so =)
LevelData Class in PasteBin: http://pastebin.com/aTKC1sBC
Without seeing more of the code it's hard to diagnose, but did you correctly initialize the choices Array before using it? Failing that I think you'll need to post more code.
Another possible issue is the delay in loading the XML data. Make sure your data is set before QuestionPane tries to access it.
When did you call
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
these? You load the XML and on complete you fill the array, what is correct. but is the XML loaded and parsed to the time when you access (fill the TextFields) the choices array already?

Resources