Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

456
Visualizações
findLastIndex() not working in FireFox, Javascript

I have an array of duplicate objects.

I have to find allways last duplicate, and remove it from array.

In Chrome i use findLastIndex, to get the index, and remove it. Sadly FireFox does not support that function.

Any other idea how to get last index of duplicate object in array, in simple way?

(to find my object I use property name: string, I dont have Id, thats why I need the Index.

I have already tried to use myArr.lastIndexOf(myArr.find((item) => item.name == itemName)); but it doesnt work.

Here is example of array:

[{
    imgSource: "/sushi/test2.png"
    ingredients: "Nori, Cucumber, Rice, Cream cheese, Sesame, Unagi sauce"
    name: "sushi1"
    price: 133
    weight: 120
  },
  {
    imgSource: "/sushi/test2.png"
    ingredients: "Nori, Cucumber, Rice, Cream cheese, Sesame, Unagi sauce"
    name: "sushi1"
    price: 133
    weight: 120
  },
  {
    imgSource: "/sushi/test3.png"
    ingredients: "Rice, Cream cheese, Sesame"
    name: "sushi3"
    price: 150
    weight: 100
  }
]

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

findLastIndex is not a js function, but I think what you are trying to do is something like this:

let arr = [
    {
        imgSource: "/sushi/test2.png",
        ingredients: "Nori, Cucumber, Rice, Cream cheese, Sesame, Unagi sauce",
        name: "sushi1",
        price: 133,
        weight: 120,
    },
    {
        imgSource: "/sushi/test2.png",
        ingredients: "Nori, Cucumber, Rice, Cream cheese, Sesame, Unagi sauce",
        name: "sushi1",
        price: 133,
        weight: 120,
    },
    {
        imgSource: "/sushi/test3.png",
        ingredients: "Rice, Cream cheese, Sesame",
        name: "sushi3",
        price: 150,
        weight: 100,
    },
];



let lastIndex = arr.lastIndexOf(arr.reverse().find(ele=>ele.name=="sushi1"))
console.log(lastIndex)

note that I used the reverse() function for find() because u need the last one.

extra note: these duplicate objects are not the same in == or === point of view, as the quality check operators for objects, check the references and not the actual value

about 4 years ago · Juan Pablo Isaza Relatório

0

I have finally found solution for my answer, thanks to Mohamad ali ghorbani, I managed to find right way to do it.

Here is how i got my last Index:

  const nameArray = myCart.map((item) => item.name);

  const indexOfItem = nameArray.lastIndexOf(itemName);

  myCart.splice(indexOfItem, 1);
about 4 years ago · Juan Pablo Isaza Relatório

0

There is no findLastIndex in JS

You can make a set

let  arr = [{
    imgSource: "/sushi/test2.png",
    ingredients: "Nori, Cucumber, Rice, Cream cheese, Sesame, Unagi sauce",
    name: "sushi1",
    price: 133,
    weight: 120
  },
  {
    imgSource: "/sushi/test2.png",
    ingredients: "Nori, Cucumber, Rice, Cream cheese, Sesame, Unagi sauce",
    name: "sushi1",
    price: 133,
    weight: 120
  },
  {
    imgSource: "/sushi/test3.png",
    ingredients: "Rice, Cream cheese, Sesame",
    name: "sushi3",
    price: 150,
    weight: 100
  }
]

const mySet = new Set()
arr.forEach(item => mySet.add(JSON.stringify(item)))
const newArr = Array.from(mySet).map(entry => JSON.parse(entry))

console.log(newArr)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda