繁体   English   中英

对象数组如何检查深度嵌套的文本字符串重复项并从数组中删除?

[英]Array of objects how do i check for deeply nested text string duplicates & remove from array?

我有一个对象数组 在这些对象的深处是一个文本字符串,我想检查同一数组中的其他对象是否具有相同的文本字符串/是否重复。 然后我需要一个删除了那些重复项的新数组。

我以为这会很简单,但它已经测试了我的智力两天了。

const arr = [
   {..obj 1}
   {..obj 2}
   {..obj 3}
   {
      id: 4,
      uid: 24872-2847-249249892842,
      tags: ['some', 'stuff'],
      type: "blogpage",
      href: "https://link-to-stuff",
      first_publication_date: "2020-02-12T16:05:04+0000",
      last_publication_date: "2020-02-18T21:52:06+0000",
      data: {
            ...some stuff
                  heading: [
                        { type: "heading1", text: "Here Is My Text I Need To Check Duplicates 
                   Of"}
                  ]
      }

   }
   {..obj 5}
   {..obj 6}
   {..obj 7}
   {..obj 8}
   {..obj 9}
   {..obj 10}
]

I figured something like:

filterOutDuplicates = (blogIndexContent) => {

let arr = blogIndexContent.pages;

let results = [];

arr.map(each => {
  if (!results || !results.length) {
    results.push(each);
  } else {
    for (let i = 0; i < results.length; i++) {
      const headline = results[i].data.heading[0].text;
      if (headline === each.data.heading[0].text) {
        return;
      } else {
        return results.push(each);
      }
    }
  }
})

console.log('Results :', results); // <-- this just gives me the same 9 blog stories again, no duplicates removed.

}


What am i doing wrong guys?

尝试这个

const arr = [
  {
    id: 4,
    data: {
      heading: [
        {
          type: "heading1",
          text: "Here Is My Text I Need To Check Duplicates Of"
        }
      ]
    }
  },
  {
    id: 5,
    data: {
      heading: [
        {
          type: "heading1",
          text: "Here Is My Text I Need To Check Duplicates Of"
        }
      ]
    }
  },
  {
    id: 6,
    data: {
      heading: [
        {
          type: "heading1",
          text: "Not Duplicates"
        }
      ]
    }
  }
];

const withoutDuplicates = arr.reduce(
  (prev, curr) =>
    prev
      .map(d => d["data"]["heading"][0]["text"])
      .includes(curr["data"]["heading"][0]["text"])
      ? [curr]
      : [...prev, curr],
  []
);
console.log(withoutDuplicates);

如果您不介意使用 lodash,可以使用_.uniqBy轻松解决

const withoutDups = _.uniqBy(arr, 'data.heading[0].text')

对您的代码稍作更改
1) 使用map删除,在数组上循环。
2) 用键构建uniq对象。 (这里的headline是我们想要的)
3) 仅当 key 不在uniq时才添加到结果数组

let arr = blogIndexContent.pages;

let results = [];

const uniq = {};

for (let i = 0; i < arr.length; i++) {
  const headline = arr[i].data.heading[0].text;
  if (!(headline in uniq)) {
    results.push(each);
    uniq[each] = 1;
  }
}

console.log("Results :", results);

这应该适合你:

filterOutDuplicates = blogIndexContent => {
  let arr = blogIndexContent.pages

  const result = []
  arr.forEach(each => {
    if (result.length === 0) {
      result.push(each)
    }
    else {
      const headline = each.data.heading[0].text
      let found = false
      for (let i = 0; i < result.length; i++) {
        if (result[i].data.heading[0].text === headline) {
          found = true
          break
        }
      }
      if (!found) {
        result.push(each)
      }
    }
  })

  console.log('Results :', results)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM