0

I want to remove any objects from my array that have a matching start and end property in reverse order.

const arr = [
  { start: 'Nebraska', end: 'Kentucky' },
  { start: 'Montana', end: 'New York' },
  { start: 'Wyoming', end: 'California' },
  { start: 'California', end: 'Wyoming' },
  { start: 'New Hampshire', end: 'Ohio' },
]

i.e., if these two objects exist in the array

{ start: 'Wyoming', end: 'California' },
{ start: 'California', end: 'Wyoming' }

I want to remove

{ start: 'California', end: 'Wyoming' }

I feel like reduce is the way to go for this solution, but not quite sure how to implement it. Any advice is appreciated

4 Answers 4

1

a simple array filter do the trick:

const arr = 
  [ { start: 'Nebraska',      end: 'Kentucky'   } 
  , { start: 'Montana',       end: 'New York'   } 
  , { start: 'Wyoming',       end: 'California' } 
  , { start: 'California',    end: 'Wyoming'    } 
  , { start: 'New Hampshire', end: 'Ohio'       } 
  ] 

const arr2 = arr.filter((c,i,a)=>
  {
  let n = a.findIndex(x=>x.start===c.end && x.end===c.start)
  return n<0 || n>i
  })
 
 console.log( arr2 )
.as-console-wrapper { max-height: 100% !important; top: 0 }

Sign up to request clarification or add additional context in comments.

Comments

1

You can easily achieve the result with Map

You can also make it one-liner

arr.filter(({ start, end }) => map.get(end) === start ? false : map.set(start, end))

const arr = [
  { start: "Nebraska", end: "Kentucky" },
  { start: "Montana", end: "New York" },
  { start: "Wyoming", end: "California" },
  { start: "California", end: "Wyoming" },
  { start: "New Hampshire", end: "Ohio" },
];

const map = new Map();
const result = arr.filter(({ start, end }) => {
  if (!(map.get(end) === start)) {
    map.set(start, end);
    return true;
  }
  return false;
});
console.log(result);

Comments

1

Here's a solution that checks whether the accumulator contains an item where the end property is equal to the current value's start property and the start property is equal to the current value's end property with the use of Array.find.

const arr=[{start:"Nebraska",end:"Kentucky"},{start:"Montana",end:"New York"},{start:"Wyoming",end:"California"},{start:"California",end:"Wyoming"},{start:"New Hampshire",end:"Ohio"}];

const result = arr.reduce((a, b) => {
  if (!a.find(e => e.end == b.start && e.start == b.end)) a.push(b);
  return a;
}, [])

console.log(result)

If you wish to overwrite when an item is found with the same end property as the start property and vice versa, you can instead use Array.findIndex to get the index and Array.splice to remove the item at that index:

const arr=[{start:"Nebraska",end:"Kentucky"},{start:"Montana",end:"New York"},{start:"Wyoming",end:"California"},{start:"California",end:"Wyoming"},{start:"New Hampshire",end:"Ohio"}];

const result = arr.reduce((a, b) => {
  let index = a.findIndex(e => e.end == b.start && e.start == b.end)
  if (index != -1) {
    a.splice(index, 1);
  }
  a.push(b);
  return a;
}, [])

console.log(result)

Comments

0

You could acheive this kind of filtering by following this simple approach :

  1. declare a variable set contains values of combination of end start
  2. filter to get only ones start end that are not in the array of set end start.
let set = [];
const result = arr.filter(({ start, end }) => {
    if(!set.includes(start+end)){
        set.push(end+start);
        return true;
    }
  return false;
});
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.