0

I have an array of all company objects

const all= [{'text': 'first'}, {'text':'second'},{'text:': 'third'}]

And also an array of the current company

const current = [{'text:': 'third'}]

Help to leave only those "text" elements that are not duplicated in array that called "all"

I try to find solution but I miss experience

const all= [{'text': 'first'}, {'text':'second'},{'text:': 'third'}]

const current = [{'text:': 'third'}]

function test(){
  const result =  all.filter(el=>{
    if(all.some(element=> el.text === element.text)){
      return false
    } else {
      return true
    }
  })
  
  console.log(result)
}

test()

!it is important that the search should be named by text as the id may differ

AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
alex
  • 137
  • 1
  • 8

1 Answers1

0
const all= [{'text': 'first'}, {'text':'second'},{'text:': 'third'}];
const current = [{'text:': 'third'}];

all.filter(ele => !current.some(cur => cur.text === ele.text))

// output
// [{text: "first"}, {text: "second"}]

There are many ways to solve this. You could use a simple for loop, but JS has built-in methods on an array type:

.filter (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

and

.some (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some)

When you read the function out, it says "filter all the array (all), if some of the current element's text is equal to that element from all, remove it (!)"

leogoesger
  • 3,476
  • 5
  • 33
  • 71