0

I have problem with find uniqueness by 2 value.
I want do something like SQL GROUP BY Tag_No and PlatformID.
I want find unique value by Tag_No and PlayformID where both value can't be duplicate

I have tried something like below, but it only works for one unique 'Tag_No'

 var NewTag = [
    {Tag_No:'xxx01',PlatformID:'12',Details:'example1'},
    {Tag_No:'xxx02',PlatformID:'13',Details:'example2'},
    {Tag_No:'xxx03',PlatformID:'14',Details:'example3'},
    {Tag_No:'xxx05',PlatformID:'5',Details:'example4'},
    {Tag_No:'xxx05',PlatformID:'12',Details:'example5'},
    {Tag_No:'xxx05',PlatformID:'12',Details:'example6'},
]
    var tmp = [];
    var result = [];
    if (NewTag !== [] /* any additional error checking */ ) {
        for (var i = 0; i < NewTag.length; i++) {
          var val = NewTag[i];
          if (tmp[val.Tag_No] === undefined ) {
             tmp[val.Tag_No] = true;
             result.push(val);
           }
          }
        }
console.log('result',result)

expected value is

result=[{Tag_No:'xxx01',PlatformID:'12',Details:'example1'},
    {Tag_No:'xxx02',PlatformID:'13',Details:'example2'},
    {Tag_No:'xxx03',PlatformID:'14',Details:'example3'},
    {Tag_No:'xxx05',PlatformID:'5',Details:'example4'},
    {Tag_No:'xxx05',PlatformID:'12',Details:'example5'},
   ]

4 Answers 4

1

You can use hash grouping approach:

 const data = [{Tag_No:'xxx01',PlatformID:'12',Details:'example'},{Tag_No:'xxx02',PlatformID:'13',Details:'example'},{Tag_No:'xxx03',PlatformID:'14',Details:'example'},{Tag_No:'xxx05',PlatformID:'5',Details:'example'},{Tag_No:'xxx05',PlatformID:'12',Details:'example'},{Tag_No:'xxx05',PlatformID:'12',Details:'example'}];

const result = Object.values(data.reduce((acc, item) => {
  const hash = [item.Tag_No, item.PlatformID].join('---');
  acc[hash] ??= item;
  return acc;
}, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }

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

Comments

1

use array.filter instead.
This filters your array on duplicates no matter what structure you have.
Reference

var NewTag = [
    {Tag_No:'xxx01',PlatformID:'12',Details:'example'},
    {Tag_No:'xxx02',PlatformID:'13',Details:'example'},
    {Tag_No:'xxx03',PlatformID:'14',Details:'example'},
    {Tag_No:'xxx05',PlatformID:'5',Details:'example'},
    {Tag_No:'xxx05',PlatformID:'12',Details:'example'},
    {Tag_No:'xxx05',PlatformID:'12',Details:'example'},
]
const uniqueArray = NewTag.filter((value, index) => {
  const _value = JSON.stringify(value);
  return index === NewTag.findIndex(obj => {
    return JSON.stringify(obj) === _value;
  });
});
console.log('result',uniqueArray)

2 Comments

wait, if Details : value is not same?.. i give an example. all details value is same, but in my case. details value all is different
That was not in your initial question. I made this for your initial question. If you want to only check for unique Tagno and platformID you should use the answer below
1

Here is my solution:

let NewTag = [
    {Tag_No:'xxx01',PlatformID:'12',Details:'example'},
    {Tag_No:'xxx02',PlatformID:'13',Details:'example'},
    {Tag_No:'xxx03',PlatformID:'14',Details:'example'},
    {Tag_No:'xxx05',PlatformID:'5',Details:'example'},
    {Tag_No:'xxx05',PlatformID:'12',Details:'example'},
    {Tag_No:'xxx05',PlatformID:'12',Details:'example'},
]
let temp=[]
let result=[];
NewTag.forEach(tag=>{
    let key=tag.Tag_No+"\t"+tag.PlatformID;
  if (!temp.includes(key)){
    temp.push(key);
    result.push(tag)
  }
});
console.log(result)

1 Comment

intresting.. i dont know why i dont concat both unique .. thx for answering
0

You could use Set to check for uniqueness

const NewTag = [
  { Tag_No: "xxx01", PlatformID: "12", Details: "example" },
  { Tag_No: "xxx02", PlatformID: "13", Details: "example" },
  { Tag_No: "xxx03", PlatformID: "14", Details: "example" },
  { Tag_No: "xxx05", PlatformID: "5", Details: "example" },
  { Tag_No: "xxx05", PlatformID: "12", Details: "example" },
  { Tag_No: "xxx05", PlatformID: "12", Details: "example" },
]

const uniquePairSet = new Set()
const res = NewTag.reduce((acc, el) => {
  const Tag_No_PlatformID = `${el.Tag_No}-${el.PlatformID}`
  if (!uniquePairSet.has(Tag_No_PlatformID)) {
    uniquePairSet.add(Tag_No_PlatformID)
    acc.push(el)
  }
  return acc
}, [])

console.log("result", res)

References

Set

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.