I am facing an issue with a JSON object that resembles the following structure:
{
name: 'test',
details: {
description: 'This is the long description',
shortDescription: 'This is the short description (ironically longer than the description!)'
}
}
Even though this example is simplified, the actual object I am working with is more complex. However, to avoid complicating the question, I have left out some details. I have a function in place that attempts to retrieve the value of a specific property within the object. Here is the code snippet:
// Private function for matching fields
var _matchField = function (item, filter) {
// Variables initialization
var text = item[filter.field],
values = filter.expression.split(',');
// Check if there is any text available
if (text) {
// Iterate through the values
angular.forEach(values, function (value) {
console.log(text);
console.log(value);
// Check for a match
if (text.toLowerCase().indexOf(value.toLowerCase()) > -1) {
// Match found
return true;
}
});
}
// No matches found
return false;
}
The problem lies in the line:
var text = item[filter.field],
If the property is simply the name, accessing it using item['name'] works fine with the given object. However, when trying to access the description like item['details.description'], it does not work as expected. I require a function that can take a property name as input and locate the corresponding property within the object to return its value. Before attempting to create one myself, I was hoping to find a straightforward solution that others may have encountered.