如何使用 Angular 获取数组中对象的索引?

问题描述 投票:0回答:6

我需要数组中对象的索引,以便我可以删除数组的这一部分。我尝试使用这个:

var index = this.urenRegistratie.indexOf(newDatum);

但它一直返回-1,我不知道为什么会发生这种情况。

这是我的代码的一部分。它从 html 表单中获取数据并将其放入我的数组中,现在我已经准备好了 if 语句( exisitingDatum ),我的代码需要位于其中。有人可以帮我一下吗?

 store(newValue:number, newDatum, newAanwezig, newComment){
    const existingDatum = this.urenRegistratie.find(billable => {
      return billable.datum === newDatum;
      return
    });

    if (!existingDatum) {
        let billable = new BillableHours();
        billable.datum = newDatum;
        billable.hours = +newValue;
        billable.aanwezig = newAanwezig;
        billable.comment = newComment;

        if(billable.aanwezig == "Aanwezig" && billable.hours !== 0 && billable.datum !== null) {
          this.urenRegistratie.push(billable);
        }
    }

    if(existingDatum) {

    }

  }
javascript arrays angular typescript indexof
6个回答
51
投票

正如 mdn 所说:

findIndex() 方法返回数组中第一个元素的索引 满足所提供的测试功能的数组。否则,它 返回-1,表示没有元素通过测试。

如果您有这样的对象数组,请尝试使用

findIndex

const a = [
    { firstName: "Adam", LastName: "Howard" },
    { firstName: "Ben", LastName: "Skeet" },
    { firstName: "Joseph", LastName: "Skeet" }
];

const index = a.findIndex(x => x.LastName === "Skeet");
console.log(index);


5
投票

添加@StepUp的答案(请选择他的最佳答案,因为它很好地回答了问题):

查找索引与 Angular 无关,而是 Vanilla JavaScript。

在您的情况下,您需要一个以

newDatum
“needle” 作为参数的函数,以便使用 findIndex:

在数组“haystack”中搜索
var getNewDatumIndex = (array, newDatumNeedle) => {
  return array.findIndex(x => x.newDatum === newDatumNeedle); 
}

如果需要,您还可以在数组原型中添加方法,以省略第一个参数:

Array.prototype.getNewDatumIndex = (newDatumNeedle) => {
  return this.findIndex(x => x.newDatum === newDatumNeedle); 
}

3
投票

一个简单而优雅的解决方案是使用 _.isEqual,它使用 lodash

执行深度比较

Npm 包 -- https://www.npmjs.com/package/lodash

const a = [
   { firstName: "Adam", LastName: "Howard" },
   { firstName: "Ben", LastName: "Skeet" },
   { firstName: "Joseph", LastName: "Skeet" }
];
const find =  { firstName: "Joseph", LastName: "Skeet" }

index = a.findIndex(x => _.isEqual(x, find));

console.log(index);

1
投票

store(newValue: number, newDatum, newAanwezig, newComment) { 让索引 = this.urenRegistratie.indexOf(existingDatum);

const existingDatum = this.urenRegistratie.find(billable => {
  return billable.datum === newDatum;
  return
});

if (index != -1) {
  let index = this.urenRegistratie.indexOf(existingDatum);
  existingDatum.splice(index, 1)
} else {

  let billable = new BillableHours();
  billable.datum = newDatum;
  billable.hours = +newValue;
  billable.aanwezig = newAanwezig;
  billable.comment = newComment;

  if (billable.aanwezig == "Aanwezig" && billable.hours !== 0 && billable.datum !== null) {
    this.urenRegistratie.push(billable);
  }
}

}


0
投票
let arr = [6, 4, 9, 5, 56, 7];
let selectedNumber = 9;

// Find the index of the selected number
let index = arr.indexOf(selectedNumber);

if (index !== -1) {
// Get previous and next values
let previousValue = index > 0 ? 
arr[index - 1] : undefined; // If it 
exists
let nextValue = index < arr.length - 1 ? 
arr[index + 1] : undefined; // If it 
exists

console.log(`Previous: ${previousValue}, 
Next: ${nextValue}`);
} else {
    console.log("Selected number not 
found.");
}

-1
投票

有时候,如果你通过id进行比较,你需要解析,因为对象的id是一个字符串。照顾这个

© www.soinside.com 2019 - 2024. All rights reserved.