Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

84
Visualizações
how to create unique array of object in deep nested array of object?

so i have a array in that a key have another array of object i need to make unique array of object from this array based on two key level and userId using lodash or vanila JavaScript
for example

const data = [
  {
    values: {
      output: [],
    }
  },
  {
    values: {
        output: [
        { level: 1, userId: "810", },
        { level: 1, userId: "811", },
        { level: 2, userId: "811", },
      ],
    },
  },
  {
    values: {
        output: [
        { level: 1, userId: "810", },
      ],
    }
  },
  {
    values: {
        output: [
        { level: 1, userId: "810", },
        { level: 3, userId: "810", },
      ],
    }
  },
  {
    values: {
        output: [
        { level: 1, userId: "810", },
        { level: 3, userId: "810", },
      ],
    }
  }
];

and expected output is

let output =[
      { level: 1, userId: "810", },
      { level: 1, userId: "811", },
      { level: 2, userId: "811", },
      { level: 3, userId: "810", },
]
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Get an array of all output properties with _.flatMap() (or Array.flatMap(), and then use _.uniqBy() to get only unique items. In the _.uniqBy() callback return a custom value by combining level and userId to a single string.

ES5:

var data = [{"values":{"output":[]}},{"values":{"output":[{"level":1,"userId":"810"},{"level":1,"userId":"811"},{"level":2,"userId":"811"}]}},{"values":{"output":[{"level":1,"userId":"810"}]}},{"values":{"output":[{"level":1,"userId":"810"},{"level":3,"userId":"810"}]}},{"values":{"output":[{"level":1,"userId":"810"},{"level":3,"userId":"810"}]}}];

var result = _.uniqBy(
  _.flatMap(data, function(o) { return o.values.output; }), 
  function(o) { return o.level + '-' + o.userId; }
);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

ES6:

const data = [{"values":{"output":[]}},{"values":{"output":[{"level":1,"userId":"810"},{"level":1,"userId":"811"},{"level":2,"userId":"811"}]}},{"values":{"output":[{"level":1,"userId":"810"}]}},{"values":{"output":[{"level":1,"userId":"810"},{"level":3,"userId":"810"}]}},{"values":{"output":[{"level":1,"userId":"810"},{"level":3,"userId":"810"}]}}];

const result = _.uniqBy(
  _.flatMap(data, o => o.values.output), 
  o => `${o.level}-${o.userId}`
);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

about 4 years ago · Juan Pablo Isaza Relatório

0

Here's a very vanilla JS solution. This won't be fast on huge data sets but for sets like your example, it's not too bad. It first gathers all of your data together into a single array and then builds a new and duplicate-free array. If you need to be careful about overwriting the data that you pass in then you can clone it first.

function mergeAllData(data) {
  var allData = [];
  for (var i in data) {
    allData = allData.concat(data[i].values.output);
  }
  return allData;
}

function filterDuplicates(allData) {
  var ret = [];
  for (var i in allData) {
    var current = allData[i];
    ret.push(current);
    for (var j in allData) {
      if (isADuplicate(current, allData[i])) {
        allData.splice(j, 1);
      }
    }
  }
  return ret;
}

function isADuplicate(t1, t2) {
  return t1.level === t2.level && t1.userId === t2.userId;
}

console.log(filterDuplicates(mergeAllData()));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda