Created
October 29, 2019 20:03
-
-
Save romaefGit/ca609285689df81c4ed748f219ba2c32 to your computer and use it in GitHub Desktop.
Javascript - Quitar items repetidos de un arreglo de objetos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Autor - Romario Augusto Estrada Flórez - romarioestrada.ff@hotmail.com | |
| * [quitarItemsRepetidos quita los items repetidos de un array con indice de tipo JSON según | |
| el campo que tu digas que debe ser único. | |
| * var data = [{ "name": "Lenovo Thinkpad 41A4298", "website": "google" }, | |
| { "name": "Lenovo Thinkpad 41A2222", "website": "google" }, | |
| { "name": "Lenovo Thinkpad 41Awww33", "website": "yahoo" }, | |
| { "name": "Lenovo Thinkpad 41A424448", "website": "google" }, | |
| { "name": "Lenovo Thinkpad 41A429rr8", "website": "ebay" }, | |
| { "name": "Lenovo Thinkpad 41A429ff8", "website": "ebay" }, | |
| { "name": "Lenovo Thinkpad 41A429ss8", "website": "rediff" }, | |
| { "name": "Lenovo Thinkpad 41A429sg8", "website": "yahoo" } | |
| ];] | |
| * @param {[Array]} data [Arreglo de items en forma de objeto JSON] | |
| * @param {[String]} campo [es el nombre del campo por el que se guía la función para elimrepetidos.] | |
| * @return {[Array]} [Arreglo sin datos repetidos según el nombre del campo] | |
| */ | |
| var quitarItemsRepetidos = function(data, campo) { | |
| var uniqueNames = []; | |
| var uniqueObj = []; | |
| for (i = 0; i < data.length; i++) { | |
| if (uniqueNames.indexOf(data[i][campo]) === -1) { | |
| uniqueObj.push(data[i]) | |
| uniqueNames.push(data[i][campo]); | |
| } | |
| } | |
| return uniqueObj; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment