如果基于值

时间:2016-09-15 15:07:56

标签: javascript arrays

最后一个问题没有得到好评,因为可能重复。再次写作解释这是如何略有不同,因为无法得到我想要的解决方案。

我有一个名为_USERS的数组,另一个名为newArray

newArray看起来像这样:

newArray = ["BLRlXKIeWkanHiSCXbBCFRTIaqk1", "sF4gWbZvAMPmrbeHsKzln7LowOx2"]

_USERS是一个对象数组,对象有一个名为useruid的属性/属性,它等于一个字符串。如果_USERS字符串与newArray中找到的任何字符串匹配,如何从useruid中删除对象。

我尝试的解决方案包括:

for (var k = 0; k < newArray.length; k++){
        if (_USERS[j].useruid == newArray[k]){
          _USERS.splice(newArray[k])
        }

var result = _.differenceWith(_USERS, newArray, _.isEqual);

这些都没有起作用,我只能把手指放在缺失的部分上

INITIAL _USERS CODE:

console.log(_USERS) => [Object, Object, Object, Object]

每个对象都有

gender: "male", name: "Rich", username: "rich@rich.com", useruid: "BLRlXKIeWkanHiSCXbBCFRTIaqk1"

newArray = ["BLRlXKIeWkanHiSCXbBCFRTIaqk1", "sF4gWbZvAMPmrbeHsKzln7LowOx2"]

newArray [0] =一个字符串。此字符串与Rich对象中的useruid匹配。因此我希望将其删除,然后发生以下情况

console.log(_USERS) => [Object, Object, Object]

5 个答案:

答案 0 :(得分:1)

对我来说,它看起来像一个简单的过滤器:

let filteredUsers = _USERS.filter(u => !newArray.includes(u.useruid))

这是在行动:

var newArray = ["BLRlXKIeWkanHiSCXbBCFRTIaqk1", "sF4gWbZvAMPmrbeHsKzln7LowOx2"]

var _USERS = [{ gender: "male", name: "Rich", username: "rich@rich.com", useruid: "BLRlXKIeWkanHiSCXbBCFRTIaqk1" }]

let filteredUsers = _USERS.filter(u => !newArray.includes(u.useruid));

/*Printing*/
/*Before*/
document.write("Before: <br>" + JSON.stringify(_USERS) + "<br><br>");
/*After*/
document.write("After <br>" + JSON.stringify(filteredUsers));

答案 1 :(得分:0)

如果我理解你的问题,这是一个简单的解决方案:

_USERS = _USERS.filter(u => newArray.indexOf(u.useruid)===-1);

答案 2 :(得分:0)

有几种干净的方法可以做到这一点,但只是评论你的splice实现。 Splice期望第一个参数是元素的索引而不是对象,因此如果调用splice(1),它将从索引1处的对象删除到数组的末尾。第二个参数是应删除的元素数,因此splice(1,1)将仅删除索引1处的元素。以下是一些可能有用的示例代码:

var newArray = ["xyz", "def"];

var _USERS = [
{useruid: "abc"},
{useruid: "def"},
{useruid: "ghi"}
];

for(var i = 0; i < _USERS.length; i++)
{
    for(var j = 0; j < newArray.length; j++)
  {
    if(_USERS[i].useruid == newArray[j])
    {
      _USERS.splice(i, 1); //remove element at index i
      i--; //decrement i because we now removed an element
      break; //move to next element
    }
  }
}

JSFiddle For Reference

答案 3 :(得分:0)

好吧只是扩展@Denys的答案。我已采用.filter方法并将其添加到jsfiddle。我只为用户提供了useruid,但这应该足以根据需要进行过滤。

答案 4 :(得分:0)

要使用循环和Array#splice改变原始数组,您需要反向运行循环,因为数组的length和数组中项目的索引在删除项目时会发生变化,因此你会错过一些项目。 ES6中的示例。

使用正向运行循环,结果不正确。

&#13;
&#13;
function customRemove(users, unwanted) {
  for (let user of users.entries()) {
    if (unwanted.includes(user[1].useruid)) {
      users.splice(user[0], 1);
    }
  }
}

const newArray = ['BLRlXKIeWkanHiSCXbBCFRTIaqk1', 'sF4gWbZvAMPmrbeHsKzln7LowOx2'];
const _USERS = [{
  useruid: 'BLRlXKIeWkanHiSCXbBCFRTIaqk1'
}, {
  useruid: 'BLRlXKIeWkanHiSCXbBCFRTIaqk1'
}, {
  useruid: 'BLRlXKIeWkanHiSCXbBCFRTIaqk2'
}, {
  useruid: 'BLRlXKIeWkanHiSCXbBCFRTIaqk1'
}];

console.log(JSON.stringify(_USERS));
customRemove(_USERS, newArray);
console.log(JSON.stringify(_USERS));
&#13;
&#13;
&#13;

使用反向运行循环,结果正确。

&#13;
&#13;
// Generator creates index number in reverse
function* reverseIndexes(arr) {
  let key = arr.length - 1;
  while (key >= 0) {
    yield key;
    key -= 1;
  }
}

// Generator like Array#entries but in reverse
function* reverseEntries(arr) {
  for (let key of reverseIndexes(arr)) {
    yield [key, arr[key]];
  }
}

function customRemove(users, unwanted) {
  // Reversed loop removing unwanted matches with Array#splice
  for (let user of reverseEntries(users)) {
    if (unwanted.includes(user[1].useruid)) {
      users.splice(user[0], 1);
    }
  }
}

const newArray = ['BLRlXKIeWkanHiSCXbBCFRTIaqk1', 'sF4gWbZvAMPmrbeHsKzln7LowOx2'];
const _USERS = [{
  useruid: 'BLRlXKIeWkanHiSCXbBCFRTIaqk1'
}, {
  useruid: 'BLRlXKIeWkanHiSCXbBCFRTIaqk1'
}, {
  useruid: 'BLRlXKIeWkanHiSCXbBCFRTIaqk2'
}, {
  useruid: 'BLRlXKIeWkanHiSCXbBCFRTIaqk1'
}];

console.log(JSON.stringify(_USERS));
customRemove(_USERS, newArray);
console.log(JSON.stringify(_USERS));
&#13;
&#13;
&#13;

相关问题