如果我有以下对象数组:

[ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]

是否有一种方法通过数组循环检查特定的用户名值是否已经存在,如果它不做任何事情,但如果它没有添加一个新对象到数组的用户名(和新ID)?

谢谢!


当前回答

接受的答案也可以这样写,在。some上使用箭头函数

 function checkAndAdd(name) {
     var id = arr.length + 1;
     var found = arr.some((el) => {
           return el.username === name;
     });
     if (!found) { arr.push({ id: id, username: name }); }
 }

其他回答

Lodash中的xorWith可以用来实现这一点

let objects = [ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]
let existingObject = { id: 1, username: 'fred' };
let newObject = { id: 1729, username: 'Ramanujan' }

_.xorWith(objects, [existingObject], _.isEqual)
// returns [ { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]

_.xorWith(objects, [newObject], _.isEqual)
// returns [ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ,{ id: 1729, username: 'Ramanujan' } ]

我喜欢Andy的回答,但是id不一定是唯一的,所以这里是我想出的创建唯一id的方法。也可以在jsfiddle上查看。请注意arr。如果之前已经删除了任何内容,length + 1可能无法保证唯一的ID。

var array = [ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' } ];
var usedname = 'bill';
var newname = 'sam';

// don't add used name
console.log('before usedname: ' + JSON.stringify(array));
tryAdd(usedname, array);
console.log('before newname: ' + JSON.stringify(array));
tryAdd(newname, array);
console.log('after newname: ' + JSON.stringify(array));

function tryAdd(name, array) {
    var found = false;
    var i = 0;
    var maxId = 1;
    for (i in array) {
        // Check max id
        if (maxId <= array[i].id)
            maxId = array[i].id + 1;

        // Don't need to add if we find it
        if (array[i].username === name)
            found = true;
    }

    if (!found)
        array[++i] = { id: maxId, username: name };
}

我假设这里的id是唯一的。Find是一个很棒的数组方法,用于检查数组中是否存在东西:

Const arr = [{id: 1,用户名:'fred'}, {id: 2,用户名:'bill'}, {id: 3,用户名:'ted'}]; 函数add(arr, name) { Const {length} = arr; Const id =长度+ 1; Const found = arr。求(el => el。用户名=== name); 如果(!发现)arr。推送({id,用户名:name}); 返回arr; } console.log(添加(arr“ted”)); console.log(添加(加勒比海盗,“黛西”));

我被赋予了一个条件来检查mysql数据库表中的数据,我的表的对象数组由id,纬度和经度作为列名,我必须检查位置是否在数据库中,否则将此插入到表中,这样: 我创建了一个由按钮调用的handle submit函数,

handle Submit = (event) => {
        const latitude = document.getElementById("latitude").innerHTML;
        const longitude = document.getElementById("longitude").innerHTML;
        const found = this.state.data.some((el) => el.latitude === latitude);
    if (!found) {
      Axios.post("http://localhost:3001/api/insert", {
        latitude: latitude,
        longitude: longitude,
      }).then(() => {
        alert("successful insert");
      });
      console.log(latitude, longitude);
    }
  };

在这里您可以看到用于插入的条件语句,如果数据库中不存在。

试试这个

第一种方法使用一些

  let arr = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' }];
    let found = arr.some(ele => ele.username === 'bill');
    console.log(found)

第二种方法使用包括、映射

   let arr = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' }];
    let mapped = arr.map(ele => ele.username);
    let found = mapped.includes('bill');
    console.log(found)