Can you please provide guidance on how to iterate through a list of addresses (address object) where the object has the property "def" set to true? Here is an example response from an API request:
{
"status": true,
"rspa": [
{
"address": "FB2A23D4-554E9",
"def": false,
"tk": false
},
{
"address": "FA0EBFB496DA-C5465A8D",
"def": true,
"tk": false
},
{
"address": "FA0434B496DA-C5465A8D",
"def": true,
"tk": false
}
]
}
The initial approach I tried is as follows:
myArray = data.rspa
var addrs = myArray.map(function(elem){
return elem.def === true ? elem.address : "";
}).join(",");
console.log(addrs);
However, this code returns all addresses, not just the ones where "def" is true. The output includes: FB2A23D4-554E9,FA0EBFB496DA-C5465A8D,FA0434B496DA-C5465A8D
Additionally, I attempted a different approach:
myArray = data.data.rspa
var idToLookFor = true;
var foundObject = myArray.filter(function(item) {
return item.def === idToLookFor;
})[0].address;
console.log(foundObject);
Unfortunately, this code leads to an error message: Uncaught TypeError: Cannot read properties of undefined (reading 'address')