value-index-pair-interface

TypeScript interface for object that represents a single array item

Usage no npm install needed!

<script type="module">
  import valueIndexPairInterface from 'https://cdn.skypack.dev/value-index-pair-interface';
</script>

README

To include in your project:

import { IValueIndexPair } from 'value-index-pair-interface/IValueIndexPair';

This is an interface for an object representing a single array item:

export interface IValueIndexPair {
    value: any;
    index: number; // integer
}

Usage Example:

You need to extract some items from an array. But not only do you need the values,
you need their indexes too, so you write this function, specifying that it returns
an array of IValueIndexPairs:

function getValueAndIndex(  
    testFunction(item, index?, array?) => boolean,  
    array  
): IValueIndexPair[]

This lets everyone know it will return an array looking something like this:

[
    {value: 'some text', index: 5},  
    {value: 10, index: 15},  
    {value: false, index: 33}  
    ...more items...
]