提问者:小点点

JS在将值推入对象之前防止对象中的重复项


如果是数组,我将使用indexof来防止重复。

但我认为indexof不能防止对象中的重复。

我使用reduce找到了这段代码。 这可能是一个解决方案,但不知道如何采用。

list.reduce((acc, current) => {
  const x = acc.find(item => item.name === current.name);
  if (!x) {
    return acc.concat([current]);
  } else {
    return acc;
  }
}, []);          
return list;

有什么帮助吗?

null

var list = [];
var data = {
  name : '',
  id : '',
  bDate : '',
  iDate : '',
  absent : [{
  reason : '',
  start: '',
  end : ''
  }]
}

function addBasicInfo(dataName, id, birthday, movein){
  var data = {
    name : dataName,
    id : id,
    bDate : birthday,
    iDate : movein,
    absent : [{
    reason : '',
    start: '',
    end : ''
    }]
  }  

  if(list.length === 0){
    console.log("[alert] Register the member data for the first time.");
    list.splice(0, 1);
    list.push(data);
    console.log(list);
    return list;
  } else if (list.length > 0) {
    console.log("[alert] More than one member data has been registered on the roll book.")     
    for(var i = 0; i < list.length; i++){
      if(list[i].name == dataName){
        console.log("[alert] The member name has been already declared. Put the number after the name to identify namesakes."); 
        return console.log(list);
      } else if(list[i].name !== dataName){
        console.log("[alert] Register new member data. ");  
        list.push(data);
        console.log(list);
        return list;
      }
    }
  }
}

addBasicInfo('Jack','Singer','1974-03-15','1993-12-01'); // line no. 27-32
addBasicInfo('Jack','Singer','1974-03-15','1993-12-01'); // line no. 34 & 35-37 (prevented to be put in an object)
addBasicInfo('Nick','Teacher','1980-01-01','1999-05-01'); // line no. 34 & 39-43 
addBasicInfo('Nick','Teacher','1980-01-01','1999-05-01'); // line no. 34 & 39-43. (it's supposed to be line no. 35-37) (not prevented to be put in an object)

null


共3个答案

匿名用户

您可以使用array.find

null

var list = [];
var data = {
  name : '',
  id : '',
  bDate : '',
  iDate : '',
  absent : [{
  reason : '',
  start: '',
  end : ''
  }]
}

function addBasicInfo(dataName, id, birthday, movein){
  var data = {
    name : dataName,
    id : id,
    bDate : birthday,
    iDate : movein,
    absent : [{
    reason : '',
    start: '',
    end : ''
    }]
  }

  // check for duplicates
  if (list.find(item => item.name === dataName)) return console.log("an item with this name already exists");

  if(list.length === 0){
    console.log("[alert] Register the member data for the first time.");
    list.splice(0, 1);
    list.push(data);
    console.log(list);
    return list;
  } else if (list.length > 0) {
    console.log("[alert] More than one member data has been registered on the roll book.")     
    for(var i = 0; i < list.length; i++){
      if(list[i].name == dataName){
        console.log("[alert] The member name has been already declared. Put the number after the name to identify namesakes."); 
        return console.log(list);
      } else if(list[i].name !== dataName){
        console.log("[alert] Register new member data. ");  
        list.push(data);
        console.log(list);
        return list;
      }
    }
  }
}

addBasicInfo('Jack','Singer','1974-03-15','1993-12-01'); // line no. 27-32
addBasicInfo('Jack','Singer','1974-03-15','1993-12-01'); // line no. 34 & 35-37 (prevented to be put in an object)
addBasicInfo('Nick','Teacher','1980-01-01','1999-05-01'); // line no. 34 & 39-43 
addBasicInfo('Nick','Teacher','1980-01-01','1999-05-01'); // line no. 34 & 39-43. (it's supposed to be line no. 35-37) (not prevented to be put in an object)

匿名用户

这可以简化

null

var list = [];


function addBasicInfo(dataName, id, birthday, movein){
  let item = list.find(pr => pr.dataName === dataName);
  
  if(!item) {
    item = {
      name : dataName,
      id : id,
      bDate : birthday,
      iDate : movein,
      absent : [{
      reason : '',
      start: '',
      end : ''
      }]
    }  
    list = [...list, item]
    return;
  }
  
  item = {
    ...item,
    name : dataName,
    id : id,
    bDate : birthday,
    iDate : movein
  }
  
  list = [...list.filter(pr => pr.dataName !== dataName), item]
}

addBasicInfo('Jack','Singer','1974-03-15','1993-12-01'); // line no. 27-32
addBasicInfo('Jack','Singer','1974-03-15','1993-12-01'); // line no. 34 & 35-37 (prevented to be put in an object)
addBasicInfo('Nick','Teacher','1980-01-01','1999-05-01'); // line no. 34 & 39-43 
addBasicInfo('Nick','Teacher','1980-01-01','1999-05-01'); // line no. 34 & 39-43. (it's supposed to be line no. 35-37) (not prevented to be put in an object)

console.log(list)

匿名用户

下面是一个可能的解决方案:

null

var list = [];

function addBasicInfo(name, id, birthday, movein){
  var data = {
    name : name,
    id : id,
    bDate : birthday,
    iDate : movein,
    absent : [{
    reason : '',
    start: '',
    end : ''
    }]
  }

  if(list.length === 0){
    console.log("[alert] Register the member data for the first time.");
  } else {
    console.log("[alert] More than one member data has been registered on the roll book.")
  }
  
  var sameMember = list.find(function(m) { return m.name === name });
  
  if (sameMember) {
    console.log("[alert] The member name has been already declared. Put the number after the name to identify namesakes."); 
  } else {
    console.log("[alert] Register new member data. ");  
    list.push(data);
  }
  
  return list;
}

addBasicInfo('Jack','Singer','1974-03-15','1993-12-01');
addBasicInfo('Jack','Singer','1974-03-15','1993-12-01');
addBasicInfo('Nick','Teacher','1980-01-01','1999-05-01');
addBasicInfo('Nick','Teacher','1980-01-01','1999-05-01');

console.log(list);