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

187
Visualizações
Why I am getting different output while filtering unique date values?

I am getting different output while filtering unique dates .. It is working correctly in some case and incorrect in some for example below..I am not able figure what is the problem..Below filtering method works good on most cases but fails in some..

I want to get all unique dates from RatingsDaily["2"] array and RatingsDaily["4"] array

// This is the data
const RatingsDaily = {
    "2": [
        {"2021-12-24": 3.21}, 
        {"2021-12-25": 3.19}, 
        {"2021-12-28": 3.29}, 
        {"2021-12-29": 3.24}, 
        {"2021-12-30": 3.38},
    ], 
    "4": [
        {"2021-12-24": 1.0}, 
        {"2021-12-25": 1.0}, 
        {"2021-12-26": 1.0}, 
        {"2022-01-27": 2.0}, 
        {"2022-01-03": 5.0}, 
        {"2022-01-05": 1.0},
    ]
}

// This is the way I am doing
let labels = []
let uniquelabel = []

for (let i = 0; i < RatingsDaily["2"].length; i++) {
    uniquelabel.push(Object.keys(RatingsDaily["2"][i]));
}

for (let i = 0; i < RatingsDaily["4"].length; i++) {
    uniquelabel.push(Object.keys(RatingsDaily["4"][i]));
}

const useFilter = arr => {
    return arr.filter((value, index, self) => {
        return self.indexOf(value) === index;
    });
};

const result = useFilter(uniquelabel);

console.log(result)

Any help would be appreciated..Thank you in advance

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

0

A short alternative:

const RatingsDaily = {"2": [{"2021-12-24": 3.21},{"2021-12-25": 3.19},{"2021-12-28": 3.29},{"2021-12-29": 3.24},{"2021-12-30": 3.38},], "4": [{"2021-12-24": 1.0},{"2021-12-25": 1.0},{"2021-12-26": 1.0},{"2022-01-27": 2.0},{"2022-01-03": 5.0},{"2022-01-05": 1.0}]}

const result = [...new Set( // will make values unique
    Object.values(RatingsDaily).flat() // lists all entries
    .map(Object.keys).flat() // retains string dates only
  )]

console.log(result);

about 4 years ago · Santiago Trujillo Relatório

0

It might be better to use a Set since it is designed to only keep unique values. Also, you may have more types of ratings, so it might be better to not hard-code 2 and 4 explicitly, but consider whatever keys are present in RatingsDaily object. One example way to do is below:

const RatingsDaily = {"2": [{"2021-12-24": 3.21},{"2021-12-25": 3.19},{"2021-12-28": 3.29},{"2021-12-29": 3.24},{"2021-12-30": 3.38},], "4": [{"2021-12-24": 1.0},{"2021-12-25": 1.0},{"2021-12-26": 1.0},{"2022-01-27": 2.0},{"2022-01-03": 5.0},{"2022-01-05": 1.0}]}

const datesSet = new Set();
for (const rating of Object.keys(RatingsDaily)) {
    for (const dateObj of RatingsDaily[rating]) {
        datesSet.add(Object.keys(dateObj)[0]);
    }
}
const datesArr = Array.from(datesSet);
console.log(datesArr);

about 4 years ago · Santiago Trujillo Relatório

0

The array uniquelabel is an array one-element sub-arrays. I don't think this is what you intended; you probably wanted an array of strings (dates), not an array of arrays. To fix that just change two lines and you'll be good.

Change:

uniquelabel.push(Object.keys(RatingsDaily[n][i]));

To:

uniquelabel.push(...Object.keys(RatingsDaily[n][i]));

In other words use the spread operator to put as string and not an array into uniquelabel.

DEMO

// This is the data
const RatingsDaily = { "2": [ {"2021-12-24": 3.21}, {"2021-12-25": 3.19}, {"2021-12-28": 3.29}, {"2021-12-29": 3.24}, {"2021-12-30": 3.38}, ], "4": [ {"2021-12-24": 1.0}, {"2021-12-25": 1.0}, {"2021-12-26": 1.0}, {"2022-01-27": 2.0}, {"2022-01-03": 5.0}, {"2022-01-05": 1.0}, ] };

// This is the way I am doing
let labels = []
let uniquelabel = []

for (let i = 0; i < RatingsDaily["2"].length; i++) {
    uniquelabel.push(...Object.keys(RatingsDaily["2"][i]));
}

for (let i = 0; i < RatingsDaily["4"].length; i++) {
    uniquelabel.push(...Object.keys(RatingsDaily["4"][i]));
}

const useFilter = arr => {
    return arr.filter((value, index, self) => {
        return self.indexOf(value) === index;
    });
};

const result = useFilter(uniquelabel);

console.log(result)

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