I have some problems mapping an array of objects in JS.
This is how the response looks in my console:
[{...}]
...and when I expand it I get this:
0:{id:0, document:{...}}
1:{id:1, document:{...}}
Usually the response I get is always without this number in front of each object, like this:
{id:0, document:{...}
{id:1, document:{...}
I tried every approach I know and I cant't manage to handle it.
The goal is to take each value out of "document" property and dynamically display it in some kind of table.
This is the way the browser devtools decides to display the array, but it is actually correct, if I'm not mistaken you're still dealing with an array.
You can verify this by logging the following:
(Replace myVar with the variable name you chose for your response array)
console.log(Array.isArray(myVar))
If it outputs true then you're fine and you are dealing with an array.
Related
I'm making a scheduling app, and storing all the scheduled things in firebase with arrays. When I try to schedule something with the same string value, it fails and doesn't add it to the array. I don't know if this is something in swift I can edit, or if it's a firebase setting.
If it's something in swift, here's the code updating the array:
doc.updateData([
"Instructor": FieldValue.arrayUnion(["\(scheduleinstructor)"])
])
If it's something in firebase, could someone please explain a way around this or a simple fix I overlooked?
According to the documentation on adding items to an array:
arrayUnion() adds elements to an array but only elements not already present
So the fact that the duplicate entry is not added is by design. If you want to allow that, you'll have to:
Read the document with the array from the databae.
Extract the array from the document into your application code.
Add the item to the array.
Write the entire modified array back to the database.
I'm receiving a response from a server-call, which I've decoded from JSON into a List
Now any of the level 1 list-values I can access, no issues. However, some of the values I'm receiving are complex, instead of a simple data-type like a String or an int, it is a Map object, with nested variables inside.
How do I get access to those?
I can't create a class to parse the Map objects into, because they could be of many different kinds, and there's no way for me to understand what they will be in advance.
Examples of a server response I could receive:
[{
info1: something,
info2: something else,
info3: something something else,
info4: [{
object1: data,
object2: data2,
object3: data3
}],
info5: something something something else,
}]
At the same time, I cannot know in advance whether or not I will be receiving a response at any given point that will contain such a map object, or which kind it will be. So how do I get access to those variables? Right now, if I type
print(list[0][info4]);
I would get
[{object1: data, object2: data2, object3: data3}]
From there, what I need to be able to do, is get the individual variable data, e.g. for object1 inside info4
data
So I have the data, but after getting the individual index, how to I access the data inside that object? What notation do I use? Everything I've tried either returns null, or type errors.
Also, the line of code I'm using to decode the server response is as follows.
List<dynamic> data2 = jsonDecode(response.body);
So the data is currently in the format of List and if I need to use some other line to decode it into a different format, or something, that might give you an idea where I'm starting from.
Aha! Managed to figure it out. It's actually because of the brackets, and bracket-types.
Effectively, the [ bracket denotes a List, whereas the { bracket denotes a Map. I didn't understand this going in, so the notations I was using were confusing.
Now that I understand that, The notation works.
So in the above example, the method I would use to get access the to object1: data result, would be as follows.
print(info[0][info4][0][object1]);
So in essence, due to the [] brackets, it's asking for an integer case to identify a given object, whereas the {} brackets are asking for a String case to identify to object instead. Once I understand this, the rest came automatically, and I was able to resolve the parsing of the data as needed.
Thank you for your insightful comments though!
I have a JSP that populates an int[] and string[] via Spring controller. When a string value for an index is "", the JSP renders all of the values from the string[].
Controller populates string[] ids & values.
form.setIds(ids);
form.setValues(values);
The JSP loops through and populates a table.
<c:forEach items="${form.ids}" varStatus="status" var="id">
<form:input path="values" value="${form.values[status.index]}" />
When string[]:values contains: {"a","b","","d"}, elements 0,1,3 show as expected: 0="a", 1="b", 3="d". Element 2 shows: "a,b,,d" for the output to the JSP for that line.
I've tried several ways to render, such as wrapping output in JSTL taglib, and changing to array lists instead of primitive string[].
I have a feeling I'm overlooking something :)
I think your bug lies elsewhere. ${form.values[status.index]} will indeed print nothing if the content of your array is really is an empty string.
This one took a while.
The issue was that using path="values" on the form input, when we hit a empty element it would show all of "values" instead of the intended blank value. The fix was to use path="values[${status.index}]" for the form input.
Also, in order to use an element, I had to switch my underlying form from using String[] objects to List objects. This was due to the form not having an initialized String[] value upon loading, whereas I could init as ArrayList without having to specify a specific length to the string array.
![information is clearly there, but not being found][1]
Does anyone have any idea what I can do to my code to make this $.each / _.each work? (I'm using both jquery and underscore, either will do)
The Array[0] concerns me. Maybe that is why the .each is getting stepped over.
The two console.log's on either side of the each log the entire object perfectly, but when I break at the each and type "regionedAps" into the console, I get an empty array.
You have an array that you are treating like an object. You should make regionedAps an object, then .each will iterate over it properly.
var regionedAps = {};
I don't think you need $.makeArray()
There is an array of objects. I'm trying to removeChild an object from that array like below. removeChild works fine but the array won't refresh itself after removing uppest object. As you can see in below, i tried to trace array items out.
Firstly, array has three items, obviously the myArray.length must be 3.
After removing a child, myArray.length must be 2, but it get 3 (Wrong).
removeChild(myArray[currShape]);
trace(myArray);
Please tell me what am i missing here.
Assuming you're using ActionScript, removeChild() only serves to take objects off the stage. It doesn't take things out of an array. You have to take the object out of the array manually in another statement.
You could try something like:
removeChild(myArray.splice(currShape,1));
This removes the entry from the array and returns that entry that will be used to remove it from the stage.