Jump to content

Search the Community

Showing results for tags '.filter()'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • W3Schools
    • General
    • Suggestions
    • Critiques
  • HTML Forums
    • CSS
  • Browser Scripting
    • JavaScript
    • VBScript
  • Server Scripting
    • Web Servers
    • Version Control
    • SQL
    • ASP
    • PHP
    • .NET
    • ColdFusion
    • Java/JSP/J2EE
    • CGI
  • XML Forums
    • XML
    • Schema
    • Web Services
  • Multimedia
    • Multimedia
    • FLASH

Find results in...

Find results that contain...

Date Created

  • Start


Last Updated

  • Start


Filter by number of...


  • Start





Website URL








Found 2 results

  1. BACKGROUND: I recently discovered the following piece of code on StackOverflow,. Although it works for a single property comparison, I do not understand it well enough to expand it to cope with multiple properties. function getUnique(inputArr, comp) { const unique = inputArr.map(e => e[comp]).map((e, i, final) => final.indexOf(e) === i && i).filter(e => inputArr[e]).map(e => inputArr[e]); return unique; } I would like to create a function that would permit the comparison of multiple values on the order of function getUnique(inputArr, comp1 [, comp2[, comp3 [...]]]){...} Any suggestions? At minimum could you help with a better understanding of how it does what it does. It maps and filters, but in a manner that I do not comprehend easily enough to manipulate. Roddy PS. A little experimentation came up with the following solution. function getUnique(inputArr, comp1, comp2) { const unique = inputArr.map(e => e[comp1] && e[comp2]) .map((e, i, final) => final.indexOf(e) === i && i) .filter(e => inputArr[e]) .map(e => inputArr[e]); return unique; } It achieves my goal, but a little more understanding is still requested.
  2. CONCERN: The following function does what it is suppose to do -- namely, select at random and without duplication a given number of elements from an array of elements. However, I fear that it is hopelessly awkward and would like your professional assessment of its construction. You will likely not find another like it on the internet, but parts of it were obtained therefrom. function selectCubes(gates) { function onlyUnique(value, index, self) { return self.indexOf(value) === index; } var cubes = ['cube_one', 'cube_two', 'cube_three', 'cube_four', 'cube_five', 'cube_six', 'cube_seven']; var selectedCubes = []; var filteredCubes = []; var n = 0; while (n < gates) { selectedCube = cubes[Math.floor(Math.random()*cubes.length)]; selectedCubes.push(selectedCube); if (filteredCubes.length < gates) { var filteredCubes = selectedCubes.filter(onlyUnique); n++; } } return filteredCubes; } console.log('selectCubes(3):' + selectCubes(3)); Please comment and have a great weekend! Roddy
  • Create New...