-2

I need to remove all keys except last inserted that have the same value on key id_ask in array but I'm learning javascript and I still do not know how to do this.

jQuery(function()
{
    let arr = []
    let q = []
    $("body").on('click', '.link_resposta', function(event)
    {
        event.preventDefault();
        /* Act on the event */

        let id_poll = $(this).data("idpesquisa")
        let id_ask = $(this).data("idpergunta")
        let id_anwser = $(this).children("li").data("idresposta")
        let q = {
            id_poll, 
            id_ask, 
            id_anwser
        }

        arr.push(q)

        console.log(arr)
    });
});

enter image description here

Marcos Vinicius
  • 207
  • 3
  • 14
  • SO is not a code writing service, you try something and come here with a question related to a specific issue. Not "I need this done and I don't know how". if you want to "learn" javascript then you need to put in the effort. – Andrei Dragotoniu May 21 '19 at 17:12
  • Possible duplicate of [How to remove array objects having property](https://stackoverflow.com/questions/20738516/how-to-remove-array-objects-having-property) – outis May 21 '19 at 17:13
  • I believe he could have multiples `id_ask` and want the latest object for each one. In this case, maybe a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) or an object that maps each `id_ask` to the latest object could be better than store multiple answers for the same `id_ask` in an array and then get the latest ones, unless you want they for some special reason. – Shidersz May 21 '19 at 17:13
  • 1
    Please post sample data as well. – Bibberty May 21 '19 at 17:19

1 Answers1

2

Using a combination of Set, Array.reverse() and Array.map we can solve this easily.

We first use the Set and we map our source array in, just feeding the id_ask field. From that we get an array of unique id_ask.

We then map the unique id_ask array and for each id_ask we call a find() on the source array in reverse.

Comments inline.

const sampleArray = [
  {
    id: 1,
    id_ask: 2,
    id_answer: 3
  },
  {
    id: 2,
    id_ask: 2,
    id_answer: 5
  },
    {
    id: 3,
    id_ask: 3,
    id_answer: 3
  },
    {
    id: 4,
    id_ask: 3,
    id_answer: 1
  },
    {
    id: 5,
    id_ask: 4,
    id_answer: 3
  }
];

// Create a unique Set of Ask ID               
const uniqueAskId = [...new Set(sampleArray.map(e => e.id_ask))]; 
console.log(uniqueAskId);

// Use Map and Reverse to get last item.
const r = uniqueAskId.map(uid => sampleArray.reverse().find(ask => ask.id_ask === uid));
console.log(r);

Here it is as a single statement:

const sampleArray = [
  {
    id: 1,
    id_ask: 2,
    id_answer: 3
  },
  {
    id: 2,
    id_ask: 2,
    id_answer: 5
  },
    {
    id: 3,
    id_ask: 3,
    id_answer: 3
  },
    {
    id: 4,
    id_ask: 3,
    id_answer: 1
  },
    {
    id: 5,
    id_ask: 4,
    id_answer: 3
  }
];

// put together in a single statement.
const result = [...new Set(sampleArray.map(e => e.id_ask))]
               .map(uid => sampleArray.reverse().find(ask => ask.id_ask === uid));

console.log(result);

NOTE: For large datasets it would obviously be more efficient to call the reverse() one time before you use.

const revArray = myArray.reverse();
const resultArray = [...new Set(revArray.map(e => e.id_ask))]
           .map(uid => revArray.reverse().find(ask => ask.id_ask === uid));
Bibberty
  • 4,670
  • 2
  • 8
  • 23