Skip to content

Instantly share code, notes, and snippets.

@joeypy
Last active March 28, 2022 18:40
Show Gist options
  • Save joeypy/a684b1b34a82e06f06b3e16dad211658 to your computer and use it in GitHub Desktop.
Save joeypy/a684b1b34a82e06f06b3e16dad211658 to your computer and use it in GitHub Desktop.

Code JS

groupBy

var groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
};

console.log(groupBy(['one', 'two', 'three'], 'length'));

// => {3: ["one", "two"], 5: ["three"]}

Iterate on an object

let perro = {
  nombre: "Scott",
  color: "Negro",
  macho: true,
  edad: 5
};

for(const [key, value] of Object.entries(perro)){
  console.log(value)
}

/*salida:
"Scott"
"Negro"
true
5
*/

Sort list

numArray.sort((a, b) => a - b); // For ascending sort numbers
numArray.sort((a, b) => b - a); // For descending sort numbers
numArray.sort() // For ascending sort string
numArray.sort().reverse() // For descending sort string
list.sort((a, b) => (a.color > b.color) ? 1 : -1) // For a property in object - ascending

Sort by two variables (date and boolean)

// Separate the sort and joined
const sortedActive = topics.filter( topic => topic.active ).sort((a, b)=> {
  return a.date > b.date ? -1 : 1
})
const sortedInactive = topics.filter( topic => !topic.active ).sort((a, b)=> {
  return a.date > b.date ? -1 : 1
})
const newSort = [...sortedTopicsActive, ...sortedTopicsInactive] //

Sort by two variables (string and string)

fruits.sort((a, b)=> {
  if (a.colA === b.colA){
    return a.colB < b.colB ? -1 : 1
  } else {
    return a.colA < b.colA ? -1 : 1
  }
})
Result > [{colA: "apple", colB: "granny smith"},
  {colA: "apple", colB: "red delicious"},
  {colA: "orange", colB: "blood"},
  {colA: "orange", colB: "clementine"},
  {colA: "orange", colB: "navel"},
  {colA": "pear", colB: "green anjou"},
  {colA: "pear", colB: "red anjou"}]

Return missing values in array 2

let a1 = [1,2,3,4,5,6];
let a2 = [1,3,5];
let absent = a1.filter(e=>!a2.includes(e));
console.log(absent); // [2,4,6]

Insert data in a list without repeating them

var a = [1,2,3], b = [4,1,5,2];

b.forEach(function(value){
  if (a.indexOf(value)==-1) a.push(value);
});

console.log(a);
// [1, 2, 3, 4, 5]

Add a element in the array if doesn't exist

temporalList = []
if (list.includes(newElement)) {
  temporalList = list.filter( (element) => element !== newElement );
} else {
  temporalList.push(id);
}

Function to create URL params passing and object if value it not null

export function params(object) {
  Object.filter = (obj, predicate) => 
    Object.keys(obj)
          .filter( key => predicate(obj[key]) )
          .reduce( (res, key) => (res[key] = obj[key], res), {} );

  let filtered = Object.filter(object, item => item !== null);

  let url = Object.keys(filtered).map(function(key) {
    let value = object[key]
    return encodeURIComponent(key) + '=' + encodeURIComponent(value)
  }).join('&')

  return ("?" + url)
}    

Forms - React

Function to validate all the inputs passing name and value

function handleInput(event) {
    let field = event.target.name;
    let value = event.target.value;
    setFormSubmit({ ...formSubmit, [field]: value })
    setErrors({ ...errors, [field]: false })
 };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment