I have the following array of object and I am trying to access tester transaction's amount and status?
I am able to display company's and project's name via the following:
transactions.map((transaction, index) => {
console.log(transaction.company_name)
console.log(transaction.project_name)
})
How would I display the status and the amount which is part of tester_transaction object?
You can access .tester_transaction
object and its properties directly from transaction
(and check if tester_transaction
is defined):
transactions.map((transaction, index) => {
console.log(transaction.company_name)
console.log(transaction.project_name)
if (transaction.tester_transaction !== undefined) {
console.log(transaction.tester_transaction.status)
console.log(transaction.tester_transaction.amount)
}
})
transactions.map((transaction, index) => {
if(transaction.hasOwnProperty("company_name")){
console.log(transaction.company_name);
}
if(transaction.hasOwnProperty("company_name")){
console.log(transaction.project_name);
}
if(transaction.hasOwnProperty("tester_transaction")){
console.log(transaction.tester_transaction.amount);
}
if(transaction.hasOwnProperty("tester_transaction")){
console.log(transaction.tester_transaction.status);
}
});
Don't forget to check for that the property exists.
transactions.map((t) => {
console.log(t. tester_transaction.amount)
console.log(t.status)
})
This code violates the use of Map.
Read more about map: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
Please use simple for of..
loop as your purpose is to just fetch the value and print it.
for(const transaction of transactions) {
console.log(transaction.company_name || '');
console.log(transaction.project_name || '');
if(!transaction. tester_transaction)
continue;
console.log(transaction.tester_transaction.amount || '');
console.log(transaction.tester_transaction.status || '');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.