WhereFieldEquals
Return all items from the list where their given 'field' attribute is equal to 'equalTo' argument E.g. !WhereFieldEquals with the following arguments: - value=[{ "name": "192.1,0.82", "type": "IP" }, { "name": "myFile.txt", "type": "File" }, { "name": "172.0.0.2", "type": "IP" }] - field='type' - equalTo='IP' - getField='name' Will return all items names where field 'type' equals 'IP' - ['192.1,0.82', '172.0.0.2'].
javascript · Filters And Transformers
Source
function toBoolean(value) { if (typeof(value) === 'string') { if (['yes', 'true'].indexOf(value.toLowerCase()) != -1) { return true; } else if (['no', 'false'].indexOf(value.toLowerCase()) != -1) { return false; } throw 'Argument does not contain a valid boolean-like value'; } return value ? true : false; } function whereFieldEquals() { // Handles the operation of finding a list of objects where the given field is present under the specified location. const valuesToSearch = argToList(args.value); const field = args.field; const equalTo = args.equalTo; const getField = args.getField; const foundMatches = []; for (const dictItem of valuesToSearch) { if (typeof dictItem === 'object' && dictItem[field] === equalTo) { if (getField) { const value = dictItem[getField]; if (value !== undefined) { foundMatches.push(value); } } else { foundMatches.push(dictItem); } } } if (foundMatches.length === 1) { return foundMatches[0]; } if (toBoolean(args.stringify || 'true')) { return JSON.stringify(foundMatches); } else { return foundMatches; } } try { return whereFieldEquals(); } catch (error) { throw 'Error occurred while running the script:\n' + error; }
README
Returns all items from the list where their given field attribute is equal to the equalTo argument.
For example, !WhereFieldEquals with the following arguments:
- value=[{ name: ‘192.1,0.82’, type: ‘IP’ }, { name: ‘myFile.txt, type: ‘File’ }, { name: ‘172.0.0.2’, type: ‘IP’ }]
- field=’type’
- equalTo=’IP’
- getField=’name’
This will return all item names where field type equals IP - [‘192.1,0.82’, ‘172.0.0.2’].
Script Data
| Name | Description |
|---|---|
| Script Type | javascript |
| Tags | transformer, general, entirelist |
Inputs
| Argument Name | Description |
|---|---|
| value | The list to apply the transformer to. |
| field | The attribute in the collection items to check equality against equalTo. |
| equalTo | The value to filter all items by in the collection. |
| getField | The field to extract from each item (Optional). |
| stringify | Whether the argument should be saved as a string (Optional). |