查找并删除JavaScript对象数组中的第一个匹配元素

ykejflvf  于 2023-04-04  发布在  Java
关注(0)|答案(8)|浏览(169)

假设我有一个数组:

members = [
        {name: 'Anna', class: 'one'}, 
        {name: 'Bob', class: 'two'},  
        {name: 'Chuck', class: 'two'}];

    removed = members.myRemoveByClass('two');     //something like

    // removed is {name: 'Bob', class: 'two'} 
    // members is [{name: 'Anna', class: 'one'}, {name: 'Chuck', class: 'two'}]

我正在寻找myRemoveByClass的东西。ES2015很好或使用Lodash。数组将已经订购。Nonequestions我有seen相当match我是looking

6qqygrtg

6qqygrtg1#

您可以使用Array.prototype.findIndex()
findIndex()方法返回数组中第一个满足测试函数的元素的 index,否则返回-1,表示没有元素通过测试。
然后splice()对象的 * 索引 *。

let members = [
        {name: 'Anna', class: 'one'}, 
        {name: 'Bob', class: 'two'},  
        {name: 'Chuck', class: 'two'}];
let idx = members.findIndex(p => p.class=="two");
if(idx >= 0){ //check if item found
  var removed = members.splice(idx, 1);     
  console.log(removed);
  console.log(members);
}
else{ //or not
  console.log('Not found!');
}
jhdbpxl9

jhdbpxl92#

您可以创建自己的Array类:

class Members extends Array {
   removeByClass(className) {
     for(const [index, member] of this.entries())
        if(member.class === className) 
           return this.splice(index, 1)[0];
   }
}

将其用作

const members = new Members([ {/*...*/}, {/*...*/} ]);
 members.removeByClass("...");
  • PS:“class”是一个非常糟糕的名称,因为它是一个保留关键字 *
rfbsl7qr

rfbsl7qr3#

这样也行

removeByClass(array, filterString) {
   const memberToRemove = array.find(element => {
     return element.class === filterString;
   });
   array.splice(array.indexOf(memberToRemove), 1);
 }

 let members = [
  { name: 'Anna', class: 'one' },
  { name: 'Bob', class: 'two' },
  { name: 'Chuck', class: 'two' }];

this.removeByClass(members, 'two');
fcy6dtqo

fcy6dtqo4#

你可以找到第一次出现的索引,并检查这个索引,然后从数组中拼接对象。

var members = [{ name: 'Anna', class: 'one' }, { name: 'Bob', class: 'two' }, { name: 'Chuck', class: 'two' }],
    index = members.findIndex(o => o.class === 'two'),
    removed = index !== -1 && members.splice(index, 1);

console.log(removed);
console.log(members);
.as-console-wrapper { max-height: 100% !important; top: 0; }
sqxo8psd

sqxo8psd5#

简而言之:

function removeByClass(obj, prop) {
    for (let i = 0; i < obj.length; i++) {
        if (obj[i].class == prop) obj.splice(i, 1);
    }   
}
cetgtptt

cetgtptt6#

您可以使用some方法来查找需要删除的member并返回true以停止循环。

let members = [
  {name: 'Anna', class: 'one'}, 
  {name: 'Bob', class: 'two'},  
  {name: 'Chuck', class: 'two'}
];

function myRemoveByClass(members, className) {
  let foundMember = null;
  members.some((member, index) => {
    if (member.class == className) {
      members.splice(index, 1);
      foundMember = member;
      return true;
    }
  });
  
  return foundMember;
}

let removed = myRemoveByClass(members, 'two');
console.log(removed);
console.log(members);

正如Jonas指出的here,你应该避免使用class,因为它是一个保留字。

55ooxyrt

55ooxyrt7#

可以使用withoutfindWhere

var arr = _.without(members , _.findWhere(members , {
 class: "two"
}));
cclgggtu

cclgggtu8#

您可以将Array.prototype.find()Array.prototype.splice()结合使用
代码示例:

const members = [{ name: 'Anna', class: 'one' }, { name: 'Bob', class: 'two' }, { name: 'Chuck', class: 'two' }];

members.find((o, i) => {
  if (o.class === 'two') {
    members.splice(i, 1); // Find and remove first matching element
    return true;
  }
  return false;
});

console.log(members);

相关问题