JS utils functions with Json array of objects

    0

    0

    Géraldine Geoffroy

    When executing the code, the first call will return false because "ddd" doesn't appear in the search input. However, the second call will return true because "xxx" appears in the search input.

    /* a function that calculates a countBy by a given key of a json array of objects and gives a json array of key-value object as output */
    function countBy(arr, key) {
      let result = [];
      let count = {};
      for (const element of arr) {
        let obj = element;
        let k = obj[key];
        if (count[k]) {
          count[k]++;
        } else {
          count[k] = 1;
        }
      }
      for (const prop in count) {
        result.push({
          key: prop,
          count: count[prop]
        });
      }
      return result;
    }
    
    /* a function that calculates a sum by a given key of a json array of objects and gives a json array of key-value object as output */
    function sumBy(arr, key) {
      let result = [];
      let sum = {};
      for (const element of arr) {
        let obj = element;
        let k = obj[key];
        if (sum[k]) {
          sum[k] += obj.price;
        } else {
          sum[k] = obj.price;
        }
      }
      for (const prop in sum) {
        result.push({
          key: prop,
          sum: sum[prop]
        });
      }
      return result;
    }
    
    /* a javascript function that is the equivalent of the python pandas crosstab method with the normalized argument */
    function crosstab(arr, key1, key2, normalized) {
      let result = [];
      let count = {};
      for (const element of arr) {
        let obj = element;
        let k1 = obj[key1];
        let k2 = obj[key2];
        if (count[k1]) {
          if (count[k1][k2]) {
            count[k1][k2]++;
          } else {
            count[k1][k2] = 1;
          }
        } else {
          count[k1] = {};
          count[k1][k2] = 1;
        }
      }
      for (const prop1 in count) {
        let obj = {};
        obj[key1] = prop1;
        for (const prop2 in count[prop1]) {
          if (normalized) {
            obj[prop2] = count[prop1][prop2] / arr.length;
          } else {
            obj[prop2] = count[prop1][prop2];
          }
        }
        result.push(obj);
      }
      return result;
    }
    
    /* a function that gives an array with the keys of a json array of objects */
    function getKeys(arr) {
      let result = [];
      for (const element of arr) {
        let obj = element;
        for (const prop in obj) {
          if (!result.includes(prop)) {
            result.push(prop);
          }
        }
      }
      return result;
    }
    
    /* a function that gives an array with the values of a json array of objects */
    function getValues(arr) {
      let result = [];
      for (const element of arr) {
        let obj = element;
        for (const prop in obj) {
          if (!result.includes(obj[prop])) {
            result.push(obj[prop]);
          }
        }
      }
      return result;
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.