This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed last month.
Why username is logging undefined? i have a user object which is coming from another screen by navigation and route but i want to call the username from that user object so I tried to do console.log(user.username) but then it logs undefined and when I only log user then it gives whole user result as expected but then why is it giving username undefined?
const { user } = route.params;
console.log(user.username)
console.log(user)
User Log Result:
LOG [{"_id": "63c42922dc60a84421e8f843", "photos": [[Object]], "profileImg": "Image uri here", "username": "Test"}]
Examine the log closely. The result is an array, not an object.
To be able to access the username, you need to access the first element of the array.
console.log(user[0].username)
I suggest you try to figure out why params.user is an array and not an object.
Related
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 months ago.
I have a script which receives a JSON formatted file as input and allows the user to call out a JSON object name for specific processing. I loop through the JSON file using a foreach() to process the specific items requested by the end user.
In this example The JSON input file has an object array called "socks". The requested JSON object name could be input by the user as "socks" as this would be the object they would like to have the code perform a function on.
I push all of these "socks" to a new array to perform the work on.
//working code
jsonDataIn.socks.forEach(function(s) {
newArray.push({ socks:s })
});
The above code functions as needed because I have hardcoded the array name as 'socks'. I cannot figure out how to assign the array name as a variable to apply the users input.
//I'd tried assigning just the array name as a variable, as well as both the data and array and those both are not valid.
let inputValueFromUser = 'socks';
let arrayNameVar=jsonDataIn.inputValueFromUser;
arrayNameVar.forEach(function(s) {
newArray.push({ [inputValueFromUser]:s })
});
nor this
let inputValueFromUser = 'socks';
let arrayNameVar=inputValueFromUser;
jsonDataIn.arrayNameVar.forEach(function(s) {
newArray.push({ [inputValueFromUser]:s })
});
In the above code the [inputValueFromUser] works for assigning the value to the newArray, but I cannot figure out how to get it to work inline as a form of 'jsonDataIn.inputValueFromUser.forEach()'. I found many examples if I were using multiple arrays in a forEach loop, but not how to pass the 'inputValueFromUser' as an array name.
I know it has to be a simple solution that I'm missing. Any assistance is appreciated.
All of the failed code that I've tried to make work has resulted in "TypeError: Cannot read properties of undefined (reading 'forEach')"
This question already has answers here:
Firebase Firestore add data without overwrite
(4 answers)
Add field separately to firestore document
(3 answers)
Closed 12 months ago.
I have a Firestore function where
admin
.firestore()
.collection('users')
.doc(context.params.userId)
.set({field1: false});
The problem is, this replaces the given user object with {field1: false}. I want to be able to add {field1: false} to the object while preserving all other existing fields, and also to be able to remove the field1 field from the object if it already exists.
How should I approach this?
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
I was trying to get data from the notes array in the data object. I'm getting the object from mongodb and I want to get the array called notes. but I can't do it. for some reason. In java there is like Object.get(notes) and I can get a specific field within the object but I don't know how to do that in js. And I haven't been able to find something that works elsewhere online.
here is my code
axios.post('/api/user/notes')
.then(res => {
console.log( Object.values(res.data));
dataSet = Object.values(res.data);
console.log(dataSet[0]);
console.log(dataSet.notes[0]);
}).catch(err => {
console.log('it didnt work' + err);
});
image
dataSet is an array. So, to get the notes, you have to first select the first element of data, dataSet[0] and then the notes array. To get the first note, it would be dataSet[0].notes[0]. Also, beware that you are setting a global variable (perhaps on a browser's window object) when you type dataSet = ... What you probably want is to declare a variable local to the function with var dataSet = ...
This question already exists:
Why isn't my future value available now? [duplicate]
Closed 6 years ago.
I'm all the time learning. My question is: why this:
let user = {
firstname: '',
secondname: ''
}
let hz = new Horizon({host: "164.132.192.28:3100"});
let table = hz('users');
hz.connect();
table.find(1).fetch().subscribe((value) => {
user = {
firstname: value.firstname,
secondname: value.secondname
}
//OR:
user.firstname = value.firstname;
user.secondname = value.secondname;
});
console.log(user);
gives me this:
And why I can't get value?:
console.log(user.firstname);
//prints nothing
My third question: how to just save the results from query into an object and use it outside the query? If I use 'return' keyword then results are similar. I know it's a newbie question but I'm really struggling with this... Can someone help me?
The following line:
table.find(1).fetch().subscribe(...);
is calling asynchronous method. This means that provided callback will be called sometime later and not immediately at the moment when subscribe is called. Therefore when it comes to console.log() the browser prints out its string representation and at that moment both first and last name are have not yet being populated - thus empty fields. When later you click on the object to see its content in console - browser evaluates it at that moment and most likely the subscribe method has already finished - so you get your first and last name present.
Your code is actually saves data in the local user variable. The thing you must remember is that you should access it only after subscribe callback has been called.
This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
JavaScript console prints assigned value of variable before it has been assigned?
(8 answers)
Closed 9 years ago.
I've got a curious situation where I am trying to update an array of objects with a new object, but when I place a console.log statement before the push, it shows that the array already has the new object inside of it. Here is the basics of the code:
var array1=[{
"Name": "Lake",
"ID": "1234"
}];
var object1={
"Name": "Mountain",
"ID": "1234"
};
function testArray() {
console.log(array1);
array1.push(object1);
}
I'd eventually like to update the original array with new information if the object contains the same ID. If it does not contain the same ID, then it should be appended. This would happen with an $.each loop over array1.
I'd greatly appreciate any help. Thank you.
It is because you are doing this in a webkit broswer like Chrome and console.log() is being queued up (it's a webkit bug, it won't happen if you do it in Firefox or non-webkit broswer), and therefore it prints a later value of the array. You need to use
JSON.stringify(array1);
for a more accurate result.
If you want to update the original array with new information only when the object contains the same ID, use an if statement to check the ID:
function updateA(obj){
if(obj.ID === array1.ID){
array1.push(obj);
console.log( JSON.stringify(array1));
}
}
updateA(object1);