I am using Amazon Web Services database dynamodb. It returns a a JSON which looks liek this:
{"Responses":{"friends":[{"to_username":"u1","from_username":"u2"}]},"UnprocessedKeys":{}}
I need to get the length of the friends array and also get individual values (e.g to_username in the first element of the array which is "u1" in the example).
I have tried something like this:
console.log(data.responses.friends.length); //get length (data is the object I get returned from my async call
console.log(data.responses.friends.to_username[0]); //get to_username of the first element in the array
Both return undefined.
Case matters!
console.log(data.Responses.friends.length); //get length (data is the object I get returned from my async call
console.log(data.Responses.friends.to_username[0]); //get to_username of the first element in the array
produces the correct results. Note the uppercase R in Responses.
Javascript is a case sensitive language. Please ensure the case in your code matches with the case in your response.
Related
I have few data like below:
Name (string),
Age (string),
Number (string),
ContactList (Array of string),
messages (Array of string),
callHistory (Array of string)
All these above values i have collected from each screen and i saved it in AsyncStorage. I want to send these data as in same format as i mentioned in bracket. But how can i do formated and send to firestore ?.I am new bie. I tried all wasy but not able to understand about doing the formating and sending to fire store.
In my firestore my collection will look like is :
Details (Collection Name) -> UserID(DocumentID) ->Data[]` (In this data i need to save as array. Like each time when i push data to this collection i need to save all in array (index format)).
How can i do that ?.
Any help would be great !
You would do this in 2 steps.
1) Get the data from AsyncStorage[1], store it in a variable in the format you want.
2) Set the variable in firestore.[2]
db.collection("Details").doc("UserID").set(variable).then(function(){
});
[1]https://reactnative.dev/docs/asyncstorage#getitem
[2]https://cloud.google.com/firestore/docs/manage-data/add-data#data_types
I do have a JSON string which I receive by ajax which is correctly ordered:
{"label":"Gr\u00f6\u00dfe","values":{"4302":"XS","4184":"S","4185":"M","4186":"L","4187":"XL","4188":"XXL","5165":"3XL","4340":"4XL"}}
This JSON fills a select. The problem is, that the options are automatically reordered ( I don't know why? ) based on the value key which means that I do not get the correct option order for the select.
The option looks like:
S,M,L,XL,XXL,XS,4XL,3XL
The correct order should be
XS,S,M,L,XL,XXL,3XL,4XL
What can I do to get the correct order?
In JavaScript there is no guaranteed order for the properties on objects. Instead, you should use an array in your JSON to ensure order. Something like this:
{"label":"Gr\u00f6\u00dfe","values":[{"4302":"XS"},{"4184":"S"}, ...]}
You can format the objects in the values array anyhow you'd like, but the idea is when concerned with order, use arrays.
In my Angular app I'm returning results via an API call, and allowing users to filter those results through a series of checkbox selections. Right now I'm running into an issue where, while results are returned as expected when one value is sent for a certain filter, when multiple values are selected (like filtering by more than one zipcode, for instance) I get zero results printed to the view. No errors, just no results.
After scratching my head for a while, using Chrome devtools network tab, I finally determined that the problem is that rather than wrapping each item in quotes in the payload - like this: "90001", "90002", what's being sent is this: "90001, 90002". In other words, quotes are wrapped around as if it were one value, not two.
This is the code, where I'm using a "join" to put together the values that are selected:
this.sendZipcode.emit(this.zipcodeFilters.text = arr.length === 0 ? undefined : arr.join(','));
I'm not sure how I can adjust the way this "join" is constructed, or find some other solution instead of "join", in order to have each item wrapped in quotes, rather than wrapped like one long string.
FYI, this is what I see in the network tab of Chrome devtools after selecting two zipcodes. As I explained, it's wrapped like one string, rather than as two values:
addresses.zipCode: {$in: ["90001, 90002"]}
Array.prototype.join will return a string. So you are converting your array to a single string which is being sent as a string. You want to send an array. Simply remove the join call and return arr directly.
String: "value, value"
Array: "value", "value"
I have two JSON objects which are Youtube API responses.
I want to append some part of the second JSON (source) to a particular position in the first JSON (destination) and finally have one merged JSON to send to view.
I've tried like so:
var merged = Object.keys(source).forEach(function(key) {
destination.items[key].contentDetails = source[key].items[key].contentDetails;
})
They both contain same number of item sets so I use the same index key for destination within the loop of source and append each to the destination JSON.
destination.items[key].contentDetails is a valid reference that returns correct value in the console but when inside this loop it's undefined.
What am I doing wrong here?
I'm not quite sure about this practice for such task so I'd greatly appreciate for some direction.
I am making a cross domain AJAX request, and because of this, I can't pass the original array from the PHP page to my HTML page. Instead, the request gets the results as a string. I have it set up so that the syntax looks like this:
([SCHOOL] Name [/SCHOOL][STATUS] Status [/STATUS])
([SCHOOL] Other name [/SCHOOL][STATUS] Other status [/STATUS])
On my HTML page, I want to be able to form an array from these different values, in the form of:
Array (
[0] =>
[0] Name
[1] Other
[1] =>
[0] Name
[1] Other status
)
This way, I can use a for loop to get specific values.
The only problem with is that split only does just that, splits things. Is there a way in JS to find the text within separators, and form an array from it? In the example again, it'd find all text within the parenthesis, and form the first array, then within that array, use the text between [SCHOOL][/SCHOOL] for the first object, and use the text between [STATUS][/STATUS] for the second object.
Ideally, you would use something more suited to storing arrays on the server side. I would recommend JSON. This would be trivial to encode using php, and decode using javascript.
If you do not have that option server side, then you can use regex to parse your text. However, you must be sure that the contents does not have your delimiters within in it.
It is not clear how you get your target data structure from your source, but I would expect something like this might work for you:
str = "([SCHOOL] Name [/SCHOOL][STATUS] Status [/STATUS])\n\
([SCHOOL] Other name [/SCHOOL][STATUS] Other status [/STATUS])"
arr =[]
m = str.match(/\(.+?\)/g)
for(i in m){
matches = m[i].match(/\(\[SCHOOL\](.+?)\[\/SCHOOL\]\[STATUS\](.+?)\[\/STATUS\]\)/)
arr.push([matches[1],matches[2]])
}
console.dir(arr)