Within a sparse-array data-structure how does one retrieve the real position count of such a structure's item?

I have a sparse array of array entries/items as follows …

var indexed = new Array();

indexed["5"] = [35, 2];
indexed["3"] = [20, 2];
indexed["4"] = [25, 3];

I want to find something like the "real" position count of an entry/item within such a sparse array structure by providing an item’s array index( respectively its key).

Expected results:

  • If one does provide 3 (either as string or number value) the returned (index) value should be the number value 0.
  • If one does provide 4 (either as string or number value) the returned (index) value should be the number value 1.
  • If one does provide 5 (either as string or number value) the returned (index) value should be the number value 2.
  • In case of an unsuccessful search the return value should be the number value -1.

10 thoughts on “Within a sparse-array data-structure how does one retrieve the real position count of such a structure's item?”

  1. A valid approach was to search the array by its find method. The callback method then needs to implement the matching criteria, which is … does the currently processed array item has the key, one is looking for

    obj => obj.hasOwnProperty(key)
    

    find then does return either the (first) matching object or it returns the undefined value. For the latter case, in order to prevent accessing a property of undefined, one might use the optional chaining/?. operator

    A final solution then might look similar to the next provided one …

    const sampleList = [{
      '2': [1, 3],
    }, {
      '3': [1, 4],
    }, {
      '4': [2, 6],
    }];
    
    function getFirstItemOfObjectByKey(arr, key) {
      return (arr.find(obj =>
        obj.hasOwnProperty(key)
      ))?.[key][0];
    }
    
    console.log(
      getFirstItemOfObjectByKey(sampleList, 4) // expected: 2
    );
    console.log(
      getFirstItemOfObjectByKey(sampleList, 3) // expected: 1
    );
    console.log(
      getFirstItemOfObjectByKey(sampleList, 1) // expected: undefined
    );
    .as-console-wrapper { min-height: 100%!important; top: 0; }

    In case one is forced to work with a somehow abusively used sparse array structure, like the OP is experiencing it, the above approach slightly changes to …

    var abusedSparseArray = new Array();
    
    abusedSparseArray["3"] = [20, 2];
    abusedSparseArray["4"] = [25, 3];
    abusedSparseArray["5"] = [35, 2];
    
    function getFirstItemOfArrayEntryByEntryKey(arr, key) {
      return (arr.find((entry, idx) =>
        idx === key
      ) || [])[0];
    }
    
    console.log(
      getFirstItemOfArrayEntryByEntryKey(abusedSparseArray, 3) // expected: 20
    );
    console.log(
      getFirstItemOfArrayEntryByEntryKey(abusedSparseArray, 4) // expected: 25
    );
    console.log(
      getFirstItemOfArrayEntryByEntryKey(abusedSparseArray, 5) // expected: 35
    );
    console.log(
      getFirstItemOfArrayEntryByEntryKey(abusedSparseArray, 1) // expected: undefined
    );
    .as-console-wrapper { min-height: 100%!important; top: 0; }

    As it finally turns out the OP wants to know the somehow real position count of an entry/item within a sparse array by looking for such an entry’s indexed key within such a sparse array structure.

    In this case one has to treat the sparse array like an ordinary (key-value) object. Passing such an object to Object.entries, one gets an array of entries, where each entry is an array which holds two items, the entry-key and the entry-value. Now one just needs to find the index / findIndex of the sparse array item’s key one is looking for …

    const sparseArray = new Array();
    
    sparseArray["5"] = [35, 2];
    sparseArray["3"] = [20, 2];
    sparseArray["4"] = [25, 3];
    
    function getRealPositionCountOfSparseArrayEntryByKey(arr, key) {
      key = String(key);
      return Object
        .entries(arr)
        .findIndex(entry => entry[0] === key);
    }
    
    console.log(
      getRealPositionCountOfSparseArrayEntryByKey(sparseArray, 3) // expected: 0
    );
    console.log(
      getRealPositionCountOfSparseArrayEntryByKey(sparseArray, 4) // expected: 1
    );
    console.log(
      getRealPositionCountOfSparseArrayEntryByKey(sparseArray, 5) // expected: 2
    );
    console.log(
      getRealPositionCountOfSparseArrayEntryByKey(sparseArray, 1) // expected: -1
    );
    .as-console-wrapper { min-height: 100%!important; top: 0; }
    Reply

Leave a Comment