@nodescript/stdlib
Version:
Standard Node Definitions
46 lines (45 loc) • 1.15 kB
JavaScript
export const module = {
moduleName: 'Array / Find By Key',
version: '1.0.2',
description: `
Finds the first occurrence of an array item where the value at specified key
equals to the specified value.
`,
keywords: ['find', 'match'],
params: {
array: {
schema: {
type: 'array',
items: { type: 'any' },
},
hideEntries: true,
},
key: {
schema: { type: 'string' },
},
value: {
schema: { type: 'any' },
},
strict: {
schema: { type: 'boolean' },
advanced: true,
},
},
result: {
schema: {
type: 'array',
items: { type: 'any' },
}
},
};
export const compute = (params, ctx) => {
const { array, key, value, strict } = params;
for (const item of array) {
const val = key === '' ? item : ctx.lib.get(item, key);
const match = strict ? val === value : ctx.lib.anyEquals(val, value);
if (match) {
return item;
}
}
return null;
};