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

113
Visualizações
Seperating a nested array by matching value in JavaScript

I have a nested array which is like this:

var data = [
              {floor: '2', id: '10002', label: 'Elutuba', items: Array(3)}
              {floor: '0', id: '10008', label: 'Saun', items: Array(2)},
              {floor: '0', id: '10010', label: 'test2', items: Array(2)},
              {floor: '0', id: '10011', label: 'test3', items: Array(2)}
          ]
          
          
  

From this array, i need to look through the matching floor values and create a arrays which matches and which doesn't. Like this:

 var data2 = [
                {floor: '0', id: '10008', label: 'Saun', items: Array(2)},
                {floor: '0', id: '10010', label: 'test2', items: Array(2)},
                {floor: '0', id: '10011', label: 'test3', items: Array(2)}
            ]
var data3 = [{floor: '2', id: '10002', label: 'Elutuba', items: Array(3)}]

How can i do that?

Here's what i have tried :

var newData = []
for (var i = 0; i < data.length; i++) {
     for (var j = 0; j < data.length; j++) {
       return  newData.push(data[i].floor === data[j].floor);
     }
 }

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

Try this code below, i don't know if it will meet your expectations

var data2 = [
                {floor: '0', id: '10008', label: 'Saun', items: Array(2)},
                {floor: '2', id: '10010', label: 'test2', items: Array(2)},
                {floor: '0', id: '10011', label: 'test3', items: Array(2)}
            ]
var data3 = [{floor: '2', id: '10002', label: 'Elutuba', items: Array(3)}]


var newData = [];

for (var i = 0; i < data2.length; i++) {

  const foundIndex = data3.findIndex( elt => elt.floor === data2[i].floor );
  
  if(foundIndex != -1){
  
    newData.push(data3[foundIndex]); // Here you can do some merge, i just take in data3
    
    data2.splice(i, 1);
    data3.splice(foundIndex, 1);
  }
  
 }
 
 console.log(newData)

about 4 years ago · Santiago Gelvez Relatório

0

I have prepared a possible solution to your problem.

const data2 = [
    {floor: '2', id: '10002', label: 'Elutuba', items: Array(3)},
    {floor: '0', id: '10008', label: 'Saun', items: Array(2)},
    {floor: '0', id: '10010', label: 'test2', items: Array(2)},
    {floor: '0', id: '10011', label: 'test3', items: Array(2)}
];

const data3 = [];

for (let i = 0; i < data2.length; i++) {
    if (data2[i].floor !== data2[i+1].floor) {
        data3.push(data2[i]);
        break;
    }
}

console.log(data3);

about 4 years ago · Santiago Gelvez Relatório

0

Welcome to Stack Overflow. Your problem is about partitioning your array into two arrays and can be solved in multiple ways, once is use the Array.reduce method and check if every array's curr element satisfies your boolean condition curr.floor === floor or not, so deciding in which of the two arrays put the element:

const data = [
    {floor: '2', id: '10002', label: 'Elutuba', items: Array(3)},
    {floor: '0', id: '10008', label: 'Saun', items: Array(2)},
    {floor: '0', id: '10010', label: 'test2', items: Array(2)},
    {floor: '0', id: '10011', label: 'test3', items: Array(2)}
];

function groupBy(data, floor) {
    const result = data.reduce(function(acc, curr) {
        acc[(curr.floor === floor) ? 0 : 1].push(curr);
        return acc;
    }, [[], []]);

    return result;
}

console.log(groupBy(data, '0'));

about 4 years ago · Santiago Gelvez 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