- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2023 08:24 AM
I need help with my 'return item.key && key.id == item.key;' line. I am required to return true then its keeps that object in keys, if return false, then it takes out that element from keys. How can I code that?
var getColor = function(item){
if(keys){
var keyColors = keys.filter(function(key){
return item.key && key.id == item.key;
});
return keyColors.length === 1 ? keyColors[0].color : {};
}
return '';
};
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2023 12:40 PM
To achieve the functionality you described, you can modify the getColor function to include the logic for adding or removing the element from the keys array based on the condition. Here's the updated function:
var getColor = function(item) {
if (keys) {
var keyColors = keys.filter(function(key) {
return item.key && key.id == item.key;
});
if (keyColors.length === 1) {
// Return the color of the matched key
return keyColors[0].color;
} else {
// If no matching key found, add the item to keys array
if (item.key) {
keys.push({ id: item.key, color: 'some_default_color' });
}
// Alternatively, you can also return a default color or an empty object
// return 'some_default_color';
// return {};
}
}
// Return an empty string or a default color if keys is not defined or empty
return '';
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2023 12:40 PM
To achieve the functionality you described, you can modify the getColor function to include the logic for adding or removing the element from the keys array based on the condition. Here's the updated function:
var getColor = function(item) {
if (keys) {
var keyColors = keys.filter(function(key) {
return item.key && key.id == item.key;
});
if (keyColors.length === 1) {
// Return the color of the matched key
return keyColors[0].color;
} else {
// If no matching key found, add the item to keys array
if (item.key) {
keys.push({ id: item.key, color: 'some_default_color' });
}
// Alternatively, you can also return a default color or an empty object
// return 'some_default_color';
// return {};
}
}
// Return an empty string or a default color if keys is not defined or empty
return '';
};