Jump to content


Popular Content

Showing content with the highest reputation on 06/07/2019 in all areas

  1. 1 point
    Here's a version if you're not into the whole brevity thing: function getUnique(inputArr, comp) { const unique = inputArr.map(function (e) { // return an array of only one property from the original array of objects (if defined) console.log(e[comp]); return e[comp]; }).map(function (e, i, final) { console.log("final", final); console.log("e", e); console.log("i", i); // return an array of either false values or indexes // this will result in an array where each value is listed only once, in the first position it appears, // and any other appearances will be false // the array will be the same length return final.indexOf(e) === i && i; }).filter(function (e) { console.log("e", e); console.log(inputArr[e]); // this will create an array of objects from the original array in each index from the previously mapped temp array // this will exclude the duplicates because those indexes in the previous array are set to false return inputArr[e]; }).map(function (e) { console.log("e", e); console.log(inputArr[e]); // this will reindex the array to remove "holes" return inputArr[e]; }); console.log(unique); return unique; } var ar = [ { 'prop': 1 }, { 'prop': 2 }, { 'prop': 1 }, { 'prop': 3 }, { 'prop': 1 }, { 'prop': 4 }, { 'prop2': 1 }, { 'prop3': 1 } ]; getUnique(ar, 'prop'); What's the purpose? This function takes an array of objects, and will return an array of those objects that have unique values for a particular property. How do multiple values fit into that? Do you have an example?
  • Create New...