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

302
Visualizações
How to check if a value in an array is present in other object and accordingly return a new object

I have an array

const dataCheck = ["Rohit","Ravi"];

I have another array of object

 const userData = [
    { name: "Sagar" },
    { name: "Vishal" },
    { name: "Rohit" },
    { name: "Ravi" },
  ];

I want to check if any value in dataCheck is present in the userData and then return a new array with the below data

const newData = [
    { name: "Sagar" },
    { name: "Vishal" },
    { name: "Rohit", status: "present" },
    { name: "Ravi", status: "present" },
  ];

I tried to do something using loops but not getting the expected results

const dataCheck = ["Rohit", "Ravi"];
const userData = [
  { name: "Sagar" },
  { name: "Vishal" },
  { name: "Rohit" },
  { name: "Ravi" }
];

let newDataValue = {};
let newData = [];
userData.forEach((user) => {
  const name = user.name;
  dataCheck.forEach((userName) => {
    if (name === userName) {
      newDataValue = {
        name: name,
        status: "present"
      };
    } else {
      newDataValue = {
        name: name
      };
    }

    newData.push(newDataValue);
  });
});

console.log(newData);

My trial gives me repeated results multiple results which is just duplicates

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You should use map() and a Set.

const dataCheck = ["Rohit","Ravi"];

const userData = [
  { name: "Sagar" },
  { name: "Vishal" },
  { name: "Rohit" },
  { name: "Ravi" },
];

const set = new Set(dataCheck);
const output = userData.map(data => set.has(data.name) ? ({...data, status: "present"}): data)
console.log(output)
.as-console-wrapper { max-height: 100% !important; top: 0; }

A Set allows for lookups in O(1) time and therefore this algorithm works in O(n) time. If you would use the array for lookups (e.g. using indcludes(), find() etc.) the runtime would be O(n²). Although this will certainly not matter at all for such small arrays, it will become more relevant the larger the array gets.

map() is used here because you want a 1:1 mapping of inputs to outputs. The only thing to determine then is, what the output should be. It is either the input, if the value is not in the Set, or it is the input extended by one property status set to "present". You can check for the presence in a Set using the has() method and can use the ternary operator ? to make the decision which case it is.

about 4 years ago · Juan Pablo Isaza Relatório

0

Loop through userData, check if name is includes in dataCheck. If true add status 'present'.

const dataCheck = ["Rohit","Ravi"];

const userData = [
  { name: "Sagar" },
  { name: "Vishal" },
  { name: "Rohit" },
  { name: "Ravi" },
];

for (let user of userData) {
  if(dataCheck.includes(user.name)) {
    user.status = 'present'
  }
}

console.log(userData)

about 4 years ago · Juan Pablo Isaza Relatório

0

const dataCheck = ["Rohit", "Ravi"];

const userData = [
  { name: "Sagar" },
  { name: "Vishal" },
  { name: "Rohit" },
  { name: "Ravi" },
];

// map through every object and check if name property
// exists in data check with help of filter. 
// if it exists the length of filter should be 1 so 
// you should return { name: el.name, status: "present" } else 
// return { name: el.name }

let newData = userData.map((el) => {
  if (dataCheck.filter((name) => name === el.name).length > 0) {
    return { name: el.name, status: "present" };
  } else {
    return { name: el.name };
  }
});

console.log("newdata: ", newData);

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