1

I am trying to find the duplicate value in the array of objects and return true value in es6 function.

arrayOne = [{
    agrregatedVal: "count",
    value: "Employee Full Name"
  },
  {
    agrregatedVal: "min",
    value: "Employee Full Name"
  },
  {
    agrregatedVal: "min",
    value: "Employee Full Name"
  },
  {
    agrregatedVal: "count",
    value: "Pay Date"
  },
  {
    agrregatedVal: "count",
    value: "Employee Full Name"
  },
  {
    agrregatedVal: "min",
    value: "Signature"
  },
  {
    agrregatedVal: "min",
    value: "Pay Date"
  }]
  
  

Above is the structure of the json

Here i have two object which are duplicate:

{
  agrregatedVal: "min",
  value: "Employee Full Name"
}, {
  agrregatedVal: "min",
  value: "Employee Full Name"
}

and remaining are not concerned as both values of each object are different.

If each object value is duplicate of another object of same array then it should return true.

I tried in this way:-

this.arrayOne = this.arrayOne.filter((thing, index, self) => {
  return index === self.findIndex((t) => {
    return t.agrregatedVal === thing.agrregatedVal && t.value === thing.value;
  });
});

it dint work, how to return true if object of each value is same of another object.

11
  • You never return anything from your callback function. It should be return index === ..., or you should remove the curly braces. Commented Jul 25, 2019 at 18:46
  • i tried to remove curly braces it still dint work for me Commented Jul 25, 2019 at 18:48
  • You must do the same for the inner callback too. jsfiddle.net/twox60dm Commented Jul 25, 2019 at 18:50
  • it removes the duplicate value and returns the array but i just need the boolean value instead of array Commented Jul 25, 2019 at 18:52
  • which boolean value? Explain clearly what your method should do: "my method should return true when...". The code you posted creates a filtered array. Commented Jul 25, 2019 at 18:57

3 Answers 3

1

Here are few things :

1) Has Duplicates :

it dint work, how to return true if object of each value is same of another object.

const dict=arrayOne.map((item,pos)=>{
  return item.agrregatedVal+":"+item.value;
});


hasDuplicate=dict.some(function(item, id){ 
     if(dict.indexOf(item) != id)
       return item;
});

console.log(hasDuplicate);

2) Remove Duplicates :

 unique = [...new Set(arrayOne.map(a => a.agrregatedVal+ "':'"+ a.value))];

Now, you can construct back your object with these

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

Comments

1

Try this:

const arrayOne = [{
    agrregatedVal: "count",
    value: "Employee Full Name"
  },
  {
    agrregatedVal: "min",
    value: "Employee Full Name"
  },
  {
    agrregatedVal: "min",
    value: "Employee Full Name"
  },
  {
    agrregatedVal: "count",
    value: "Pay Date"
  },
  {
    agrregatedVal: "count",
    value: "Employee Full Name"
  },
  {
    agrregatedVal: "min",
    value: "Signature"
  },
  {
    agrregatedVal: "min",
    value: "Pay Date"
  }]

function hasDuplicate(arrayOne) {
  const newArray = arrayOne.filter((thing, index, self) => index === self.findIndex(t => t.agrregatedVal === thing.agrregatedVal && t.value === thing.value));

  return newArray.length < arrayOne.length;
}

console.log(hasDuplicate(arrayOne));

hasDupliecate will return true if there is/are duplicates

Comments

1

De-duplicating an array

Consider the following implementation:

const initialArray = [
  {agrregatedVal: "count", value: "Employee Full Name"},
  {agrregatedVal: "min", value: "Employee Full Name"},
  {agrregatedVal: "min", value: "Employee Full Name"},
];

const dedupedArray = [
  ...new Set(initialArray.map(item => JSON.stringify(item)))
].map(itemJson => JSON.parse(itemJson));

console.log(dedupedArray);

  1. Every item in the original array is converted to Json string
  2. The strings are dumped inside a Set which discards the duplicates
  3. The Set, which now contains only unique Json strings, is converted back into an array
  4. The objects are recreated back from the strings

A function which determines if an object is duplicated inside an array

I think what you actually want to achieve is to have a function that determines if an object is a duplicate of another object in an array, right?

const array = [
  {agrregatedVal: "count", value: "Employee Full Name"},
  {agrregatedVal: "min", value: "Employee Full Name"},
  {agrregatedVal: "min", value: "Employee Full Name"},
];

const isDuplicate = (itemA, array) => !!array.find(itemB => itemA !== itemB && JSON.stringify(itemA) === JSON.stringify(itemB));

console.log(isDuplicate(array[1], array))

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.