Saya telah mengikuti

let arr = [
  { id: 1, referenceId: 1, type: "normal", name: "a" },
  { id: 2, referenceId: 1, type: "normal", name: "b" },
  { id: 3, referenceId: 3, type: "chat", name: "c" },
  { id: 4, referenceId: 4, type: "normal", name: "d" },
  { id: 5, referenceId: 5, type: "chat", name: "e" },
  { id: 6, referenceId: 3, type: "chat", name: "f" }
];

Saya ingin output seperti berikut:

[
  { id: 1, referenceId: 1, type: "normal", name: "a" },
  { id: 2, referenceId: 1, type: "normal", name: "b" },
  { id: 4, referenceId: 4, type: "normal", name: "d" },
  { id: 5, referenceId: 5, type: "chat", name: "e" },
  { id: 6, referenceId: 3, type: "chat", name: "f" }
];

Saya ingin mengurutkan if type === "chat", Coba seperti ini(type === "normal", no sort):

arr.filter(item => {
  if (item.type === "normal") {
    return item
  }
  //sort array by referenceId and keeping last item

})
-1
PanchaLawther 9 Januari 2021, 07:32

3 jawaban

Jawaban Terbaik

Anda dapat membuat pemetaan referenceId -> lastIndex, lalu mencari indeks di filter Anda untuk menyimpan hanya indeks yang cocok dengan yang terakhir untuk referenceId tersebut. Pemeriksaan dilakukan untuk memastikan kami hanya memfilter type: "chat" item. Kompleksitas waktu bersifat linier.

const arr = [
  { id: 1, referenceId: 1, type: "normal", name: "a" },
  { id: 2, referenceId: 1, type: "normal", name: "b" },
  { id: 3, referenceId: 3, type: "chat", name: "c" },
  { id: 4, referenceId: 4, type: "normal", name: "d" },
  { id: 5, referenceId: 5, type: "chat", name: "e" },
  { id: 6, referenceId: 3, type: "chat", name: "f" }
];

const lastIdxes = arr.reduce((a, e, i) => {
  a[e.referenceId] = i;
  return a;
}, {});
const result = arr.filter((e, i) => 
  e.type !== "chat" || i === lastIdxes[e.referenceId]
);
console.log(result);
0
ggorlen 9 Januari 2021, 05:16

Anda dapat menggunakan pengurangan seperti ini,

let arr = [
  { id: 1, referenceId: 1, type: "normal", name: "a" },
  { id: 2, referenceId: 2, type: "normal", name: "b" },
  { id: 3, referenceId: 3, type: "chat", name: "c" },
  { id: 4, referenceId: 4, type: "normal", name: "d" },
  { id: 5, referenceId: 5, type: "chat", name: "e" },
  { id: 6, referenceId: 3, type: "chat", name: "f" }
];

res = arr.reduce((prev, curr) => {
  index = prev.findIndex(item => item.referenceId === curr.referenceId);
  if(index > -1) {
    prev.splice(index, 1);
  }
  
  prev.push(curr);
  return prev;
}, []);
console.log(res);
0
sabbir.alam 9 Januari 2021, 04:41

Urutkan berdasarkan id seperti yang ditunjukkan dalam judul pertanyaan ini, lalu lacak kapan duplikatnya akan dijatuhkan

// sort by id and drop referenceId duplicates
const arr = [
  { id: 1, referenceId: 1, type: "normal", name: "a" },
  { id: 2, referenceId: 2, type: "normal", name: "b" },
  { id: 3, referenceId: 3, type: "chat", name: "c" },
  { id: 4, referenceId: 4, type: "normal", name: "d" },
  { id: 5, referenceId: 5, type: "chat", name: "e" },
  { id: 6, referenceId: 3, type: "chat", name: "f" }
];

arr.sort((a, b) => a.id > b.id ? 1 : -1);

const toDrop = [];
const mapper = {};
arr.forEach((curr, i) => {
    if (mapper[curr.referenceId] !== undefined) {
    toDrop.unshift(mapper[curr.referenceId]);
  }
  mapper[curr.referenceId] = i;
});

toDrop.forEach(i => {
    arr.splice(i, 1);
});

console.log(arr);
0
D. Seah 9 Januari 2021, 04:56