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

Javascript 对象数组如何检查深度嵌套的文本字符串重复项&;从阵列中删除?,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我有一个对象数组 在这些对象的深处是一个文本字符串 我想检查同一数组中的其他对象是否具有相同的文本字符串/重复。 然后我需要一个新的阵列,删除这些重复项 我原以为这很简单,但这两天来一直在考验我的智力 const arr = [ {..obj 1} {..obj 2} {..obj 3} { id: 4, uid: 24872-2847-249249892842, tags: ['some', 'stuff'], type:

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

我原以为这很简单,但这两天来一直在考验我的智力

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)=>{

试试这个

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,那么使用

const without dups=u.uniqBy(arr,'data.heading[0].text'))

对代码的细微更改
1) 使用
map
删除,并在数组上进行循环。
2) 用键构建
uniq
对象。(这里
headline
就是我们想要的)
3) 仅当键不在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);
让arr=blogIndexContent.pages;
让结果=[];
const uniq={};
for(设i=0;i
这应该适合您:

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)
}
filterOutDuplicates=blogIndexContent=>{
设arr=blogIndexContent.pages
常量结果=[]
arr.forEach(每个=>{
如果(result.length==0){
结果:推送(每个)
}
否则{
const headline=each.data.heading[0]。文本
设为假
for(设i=0;i
幸运的是,我们已经在其他地方的应用程序中使用了lodash,所以这非常完美。非常感谢!
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)
}