Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
How to find unique value with id in JavaScript?

How to find unique value with id in JavaScript? right now I'm getting just value.

I'm getting:-

[
  "Care One",
  "Care Two"
]

I need:-

[
{
    "Source_ID": 1,
    "SourceName_CR": "Care One"
},
{
    "Source_ID": 2,
    "SourceName_CR": "Care Two"
}

]

My code:-

const body = [
{
            Field_ID: 1,
            FieldName_CR: "First Name",
            Source_ID: 1,
            SourceName_CR: "Care One"
        },
        {
            Field_ID: 2,
            FieldName_CR: "Last Name",
            Source_ID: 1,
            SourceName_CR: "Care One"
        }, {
            Field_ID: 3,
            FieldName_CR: "Phone",
            Source_ID: 2,
            SourceName_CR: "Care Two"
        },
        {
            Field_ID: 4,
            FieldName_CR: "Email",
            Source_ID: 2,
            SourceName_CR: "Care Two"
        },  ];

console.log([...new Set(body.map(item => item.SourceName_CR))]);

almost 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

A Set won't work here because you want to deduplicate objects - but objects can't be compared with each other that way. Doing just .map(item => item.SourceName_CR) doesn't make sense either because then you only get the string (eg Care Two) and not the whole object you want.

Group the source names by the source IDs to deduplicate, then turn the entries of the resulting object into the required structure.

const body = [
{
    Field_ID: 1,
    FieldName_CR: "First Name",
    Source_ID: 1,
    SourceName_CR: "Care One"
},
{
    Field_ID: 2,
    FieldName_CR: "Last Name",
    Source_ID: 1,
    SourceName_CR: "Care One"
}, {
    Field_ID: 3,
    FieldName_CR: "Phone",
    Source_ID: 2,
    SourceName_CR: "Care Two"
},
{
    Field_ID: 4,
    FieldName_CR: "Email",
    Source_ID: 2,
    SourceName_CR: "Care Two"
},  ];
const namesBySourceID = Object.fromEntries(
  body.map(obj => [obj.Source_ID, obj.SourceName_CR])
);
const result = Object.entries(namesBySourceID)
  .map(([Source_ID, SourceName_CR]) => ({ Source_ID, SourceName_CR }));
console.log(result);

almost 4 years ago · Santiago Trujillo Denunciar

0

This would also work:

const body = [ { Field_ID: 1, FieldName_CR: "First Name", Source_ID: 1, SourceName_CR: "Care One", }, { Field_ID: 2, FieldName_CR: "Last Name", Source_ID: 1, SourceName_CR: "Care One", }, { Field_ID: 3, FieldName_CR: "Phone", Source_ID: 2, SourceName_CR: "Care Two", }, { Field_ID: 4, FieldName_CR: "Email", Source_ID: 2, SourceName_CR: "Care Two", }, ];

const uniqueItems = [];

body.forEach(({Source_ID, SourceName_CR}) => {
    const found = uniqueItems.find(o => o.Source_ID === Source_ID);
    if(!found){
        uniqueItems.push({Source_ID, SourceName_CR});
    }
})

console.log(uniqueItems);

almost 4 years ago · Santiago Trujillo Denunciar

0

const body = [
{
            Field_ID: 1,
            FieldName_CR: "First Name",
            Source_ID: 1,
            SourceName_CR: "Care One"
        },
        {
            Field_ID: 2,
            FieldName_CR: "Last Name",
            Source_ID: 1,
            SourceName_CR: "Care One"
        }, {
            Field_ID: 3,
            FieldName_CR: "Phone",
            Source_ID: 2,
            SourceName_CR: "Care Two"
        },
        {
            Field_ID: 4,
            FieldName_CR: "Email",
            Source_ID: 2,
            SourceName_CR: "Care Two"
        },  ];
        
const key = 'SourceName_CR';

const uniqValues = [...new Map(body.map(item =>[item[key], item])).values()];

const onlyIDandNames=uniqValues.map((item)=>({Source_ID:item?.Source_ID,SourceName_CR:item?.SourceName_CR}))

console.log(uniqValues)
console.log(onlyIDandNames)

almost 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda