skip to Main Content

I am not sure what I am missing from my function here to remove elements in a a set. It seems the loop is not working as expected. If you have a more optimal solution O(1), please share. Thanks!

const dupArr = ["a", "b", "c", "d", "d",'dog']

function transformSearchFields(fields) {
  let noDupes = new Set(fields)
  const searchData = new Set(['dog', 'a'])

  // iterate over noDupes set and check if element is in the searchData set. If it is, remove the element from the noDupes set
  noDupes.forEach(element => {
    if (element in searchData) {
        noDupes.delete(element)
    }
  })

  return [...noDupes]
  }
// desired output: ["b", "c", "d"]

I expected the loop to remove values that were in the searchData set

4

Answers


  1. Here is example how you can filter an array with unique values in response:

    const inputArray = ['a', 'b', 'c', 'd', 'd', 'dog'];
    const dataToReject = ['dog', 'a'];
    
    const filteredUniqueArray = [...new Set(
      inputArray.filter(inputArrayElement =>
        !dataToReject.some(dataToRejectElement => 
          inputArrayElement == dataToRejectElement
        )
      )
    )];
    
    console.log(filteredUniqueArray); // ['b', 'c', 'd']
    Login or Signup to reply.
  2. To check if a value is "in" a Set, use .has()

    .has

    The has() method returns a boolean indicating whether an element with the specified value exists in a Set object or not.

    In your case this would be:

    if (searchData.has(element)) {
    

    Updated snippet

    const dupArr = ["a", "b", "c", "d", "d", 'dog']
    
    function transformSearchFields(fields) {
      let noDupes = new Set(fields)
      const searchData = new Set(['dog', 'a'])
    
      // iterate over noDupes set and check if element is in the searchData set. If it is, remove the element from the noDupes set
      noDupes.forEach(element => {
        //console.log(element, element in searchData, searchData.has(element))
        if (searchData.has(element)) {
          noDupes.delete(element)
        }
      })
    
      return [...noDupes]
    }
    console.log(transformSearchFields(dupArr));

    There are other methods filter data (as provided in other answer, so not repeated here).

    Login or Signup to reply.
  3. You could take the Set directly and omit items of this the for filtering.

    function transformSearchFields(fields) {
        return fields.filter(
            (s => v => !s.has(v) && s.add(v))
            (new Set(['dog', 'a']))
        );
    }
    
    console.log(transformSearchFields(["a", "b", "c", "d", "d", 'dog'])); // b c d
    Login or Signup to reply.
  4. You can convert your dupArr Set to array again with spread operator and use filter with the has method for your searchData Set

    const transform = (fields, searchData) => [...new Set(fields)].filter(el => !searchData.has(el));
    
    
    const searchData = new Set(['dog', 'a']);
    const dupArr = ["a", "b", "c", "d", "d",'dog']
    
    console.log(transform(dupArr,searchData)); 
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search