我有这个对象数组,其中有很多重复的条目。我可以清理数组并删除重复的数组,但问题是我需要根据属性的前 3 个单词删除那些匹配的数组。
假设这是数组:
let arr = [
{
text: "Be good and you will be lonely. But there’s nothing wrong with being lonely.",
id: 1
},
{
text: "Coffee is a way of stealing time.",
id: 2
},
{
text: "Be good and you will be lonely. But there’s nothing wrong with being lonely.",
id: 3
}
];
我想匹配每个文本的前 3 个单词,如果匹配,则从旧数组中删除匹配的对象之一,并将删除的对象推送到新数组。
到目前为止,我可以用这段代码删除重复的代码,但我不知道下一步该怎么做。
let texts = {};
arr = arr.filter(function(currentObject) {
if (currentObject.text in seenNames) {
return false;
} else {
seenNames[currentObject.text] = true;
return true;
}
});
如果有人指出我正确的方向,那将是一个很大的帮助。
更新:
我用与以前不同的方法重新开始了整个事情。正如@Andreas 和@freedomn-m 所说,我根据前 3 个单词拆分项目,然后尝试通过匹配拆分项目来过滤原始数组。但是现在我无需任何过滤就可以恢复所有值。
let arr = [{
"text": "Be good and you will be lonely. But there’s nothing wrong with being lonely.",
"id": 1
},
{
"text": "Coffee is a way of stealing time.",
"id": 2
},
{
"text": "Be good and you will be lonely. But there’s nothing wrong with being lonely.",
"id": 3
}
];
let removedItems = [];
let filtered = arr.filter((item, index) => {
let splitItem = item["text"].split(" ").slice(0, 3).join(" ").toLowerCase();
if (item["text"].toLowerCase().startsWith(splitItem, index + 1)) {
return item;
} else {
removedItems.push(item);
}
});
console.log(filtered);
console.log(removedItems);
答案 0 :(得分:1)
对于如何获得3个单词的原始问题,一种选择是使用 .split() .slice() 和 .join():
var firstWords = item["text"].split(" ").slice(0, 3).join(" ");
然后,您可以直接将原始问题中的 currentObject.text
替换为 firstWords
:
let texts = {};
arr = arr.filter(function(currentObject) {
if (firstWords in seenNames) {
return false;
} else {
seenNames[firstWords] = true;
return true;
}
});
更新尝试这样做,但有两个问题:
.filter(function(item))
必须返回 true/false(就像它最初所做的那样)而不是 item
/nothing。
item["text"].toLowerCase().startsWith(splitItem)
将始终为真,因为 splitItem 是从 item["text"] 构建的
将 removedItems
附加列表添加到原始列表中:
let arr = [{
"text": "Be good and you will be lonely. But there’s nothing wrong with being lonely.",
"id": 1
},
{
"text": "Coffee is a way of stealing time.",
"id": 2
},
{
"text": "Be good and you will be lonely. But there’s nothing wrong with being lonely.",
"id": 3
}
];
let removedItems = [];
let seenNames = {};
let filtered = arr.filter((item, index) => {
let splitItem = item["text"].split(" ").slice(0, 3).join(" ").toLowerCase();
if (splitItem in seenNames) {
// already exists, so don't include in filtered, but do add to removed
removedItems.push(item);
return false;
}
// doesn't exist, so add to seen list and include in filtered
seenNames[splitItem] = true;
return true;
});
console.log(filtered);
console.log(removedItems);