我有這樣的物件:
cons data = []
const result =
[
{
"result_id": "AAA877",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAA877",
"hashtag_id": 1,
"apptype_id": 3,
"tag": {
"id": 1,
"name": "NodeJs",
"hashtag_group_id": 1
}
}
]
},
{
"result_id": "AAAAA1",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAAAA1",
"hashtag_id": 1,
"apptype_id": 4,
"tag": {
"id": 1,
"name": "NodeJs",
"hashtag_group_id": 1
}
}
]
},
{
"result_id": "AAB238",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAB238",
"hashtag_id": 2,
"apptype_id": 4,
"tag": null
}
]
},
{
"result_id": "AAB415",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAB415",
"hashtag_id": 1,
"apptype_id": 3,
"tag": {
"id": 1,
"name": "NodeJs",
"hashtag_group_id": 1
}
}
]
},
{
"result_id": "AAD668",
"emp_id": 2,
"hashtag": [
{
"result_id": "AAD668",
"hashtag_id": 1,
"apptype_id": 3,
"tag": {
"id": 1,
"name": "NodeJs",
"hashtag_group_id": 1
}
}
]
},
{
"result_id": "AAG239",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAG239",
"hashtag_id": 4,
"apptype_id": 3,
"tag": null
}
]
},
{
"result_id": "AAH740",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAH740",
"hashtag_id": 2,
"apptype_id": 3,
"tag": null
}
]
},
{
"result_id": "AAK119",
"emp_id": 2,
"hashtag": [
{
"result_id": "AAK119",
"hashtag_id": 1,
"apptype_id": 4,
"tag": {
"id": 1,
"name": "NodeJs",
"hashtag_group_id": 1
}
}
]
},
{
"result_id": "AAK298",
"emp_id": 1,
"hashtag": [
{
"result_id": "AAK298",
"hashtag_id": 2,
"apptype_id": 3,
"tag": null
}
]
}
]
我想過濾和推送emp_id并且apptype_id沒有重復
這是我所期望的:
[
{ emp_id: 1, app_type_id: 3 },
{ emp_id: 1, app_type_id: 4 },
{ emp_id: 2, app_type_id: 3 },
{ emp_id: 2, app_type_id: 4 }
]
我正在嘗試這樣:
result.forEach(r => {
if (r.hashtag[0].tag !== null) {
const t = {
emp_id: r.emp_id,
app_type_id: r.hashtag[0].apptype_id
}
if (data.indexOf(t) === -1) {
data.push(t)
}
}
})
但我得到的是這樣的:
[
{ emp_id: 1, app_type_id: 3 },
{ emp_id: 1, app_type_id: 4 },
{ emp_id: 1, app_type_id: 3 },
{ emp_id: 2, app_type_id: 3 },
{ emp_id: 2, app_type_id: 4 }
]
如何像我預期的那樣過濾沒有重復?
如果還不夠,請詢問我是否需要更多資訊
uj5u.com熱心網友回復:
另一種方法。回圈資料,將您想要的屬性解構為一個新物件,將其字串化,然后將該字串推送到一個臨時陣列中。然后,在每次下一次迭代中,檢查該字串是否已經創建。如果它沒有將該字串推入陣列。
最后map遍歷字串并決議每個字串以生成與預期輸出匹配的物件陣列。
const data=[{result_id:"AAA877",emp_id:1,hashtag:[{result_id:"AAA877",hashtag_id:1,apptype_id:3,tag:{id:1,name:"NodeJs",hashtag_group_id:1}}]},{result_id:"AAAAA1",emp_id:1,hashtag:[{result_id:"AAAAA1",hashtag_id:1,apptype_id:4,tag:{id:1,name:"NodeJs",hashtag_group_id:1}}]},{result_id:"AAB238",emp_id:1,hashtag:[{result_id:"AAB238",hashtag_id:2,apptype_id:4,tag:null}]},{result_id:"AAB415",emp_id:1,hashtag:[{result_id:"AAB415",hashtag_id:1,apptype_id:3,tag:{id:1,name:"NodeJs",hashtag_group_id:1}}]},{result_id:"AAD668",emp_id:2,hashtag:[{result_id:"AAD668",hashtag_id:1,apptype_id:3,tag:{id:1,name:"NodeJs",hashtag_group_id:1}}]},{result_id:"AAG239",emp_id:1,hashtag:[{result_id:"AAG239",hashtag_id:4,apptype_id:3,tag:null}]},{result_id:"AAH740",emp_id:1,hashtag:[{result_id:"AAH740",hashtag_id:2,apptype_id:3,tag:null}]},{result_id:"AAK119",emp_id:2,hashtag:[{result_id:"AAK119",hashtag_id:1,apptype_id:4,tag:{id:1,name:"NodeJs",hashtag_group_id:1}}]},{result_id:"AAK298",emp_id:1,hashtag:[{result_id:"AAK298",hashtag_id:2,apptype_id:3,tag:null}]}];
const temp = [];
for (const obj of data) {
const { emp_id, hashtag: [{ apptype_id }] } = obj;
const newObj = { emp_id, app_type_id: apptype_id };
const str = JSON.stringify(newObj);
if (!temp.includes(str)) temp.push(str);
}
const out = temp.map(str => JSON.parse(str));
console.log(out);
uj5u.com熱心網友回復:
在提取資訊之前,我會對您擁有的物件運行過濾器!這應該可以解決問題:
var removed_duplicates = result.filter((result, index, self) =>
index === self.findIndex((other_entry) =>
(other_entry.emp_id === result.emp_id &&
other_entry.hashtag[0].apptype_id === result.hashtag[0].apptype_id)))
它的作用是通過在串列中查找具有與您嘗試過濾的相同值的其他條目來過濾您的原始條目。如果沒有,則回傳的索引將相同,否則第一次遇到一個重復條目將成功,但對于以下所有條目均失敗,導致它們被洗掉。
uj5u.com熱心網友回復:
您可以獲得一組物件并使用Set.
const
data = [{ result_id: "AAA877", emp_id: 1, hashtag: [{ result_id: "AAA877", hashtag_id: 1, apptype_id: 3, tag: { id: 1, name: "NodeJs", hashtag_group_id: 1 } }] }, { result_id: "AAAAA1", emp_id: 1, hashtag: [{ result_id: "AAAAA1", hashtag_id: 1, apptype_id: 4, tag: { id: 1, name: "NodeJs", hashtag_group_id: 1 } }] }, { result_id: "AAB238", emp_id: 1, hashtag: [{ result_id: "AAB238", hashtag_id: 2, apptype_id: 4, tag: null }] }, { result_id: "AAB415", emp_id: 1, hashtag: [{ result_id: "AAB415", hashtag_id: 1, apptype_id: 3, tag: { id: 1, name: "NodeJs", hashtag_group_id: 1 } }] }, { result_id: "AAD668", emp_id: 2, hashtag: [{ result_id: "AAD668", hashtag_id: 1, apptype_id: 3, tag: { id: 1, name: "NodeJs", hashtag_group_id: 1 } }] }, { result_id: "AAG239", emp_id: 1, hashtag: [{ result_id: "AAG239", hashtag_id: 4, apptype_id: 3, tag: null }] }, { result_id: "AAH740", emp_id: 1, hashtag: [{ result_id: "AAH740", hashtag_id: 2, apptype_id: 3, tag: null }] }, { result_id: "AAK119", emp_id: 2, hashtag: [{ result_id: "AAK119", hashtag_id: 1, apptype_id: 4, tag: { id: 1, name: "NodeJs", hashtag_group_id: 1 } }] }, { result_id: "AAK298", emp_id: 1, hashtag: [{ result_id: "AAK298", hashtag_id: 2, apptype_id: 3, tag: null }] }],
result = data
.flatMap(({ emp_id, hashtag }) => hashtag.map(({ apptype_id }) => ({ emp_id, apptype_id })))
.filter((s => ({ emp_id, apptype_id }) => {
const key = [emp_id, apptype_id].join('|');
if (!s.has(key)) return s.add(key);
})(new Set));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
uj5u.com熱心網友回復:
JS 中沒有結構比較,物件通過“記憶體中的地址”進行比較,您必須比較原始值([].indexOf(t)將陣列的項與 比較t)。
console.log({text: 'hey'} !== {text: 'hey'})
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/472967.html
標籤:javascript 节点.js 目的
