-2

I want all the name1 and name2 to be unique in every sub-array

    [
        { id: 1, name1: a, name2: b },
        { id: 2, name1: c, name2: d },
        { id: 3, name1: e, name2: a }
        
    ],
    [
        { id: 4, name1: t, name2: k },
        { id: 5, name1: j, name2: h },
        { id: 6, name1: z, name2: x },
    ],
]

In this example id-1 name1 and id-3 name2 have the value 'a', so I pushed id-3 to the next sub-array where there are no name1 and name2 matching id-3s name1 and name2.

    [
        { id: 1, name1: a, name2: b },
        { id: 2, name1: c, name2: d },
        
    ],
    [
        { id: 4, name1: t, name2: k },
        { id: 5, name1: j, name2: h },
        { id: 6, name1: z, name2: x },
        { id: 3, name1: e, name2: a },
    ],
]

If there's no such sub-array where id-3 name1 and name2 will be unique I want to create a new sub array and push id-3 into that.

2

2 Answers 2

1

You can build functions which determine whether a particular object is a "duplicate", then iterate your array, filtering out the duplicates and pushing them to the next entry in the array. If they are also a dupe in that entry, they will be pushed to the next entry, and so on...

const data = [
    [
        { id: 1, name1: 'a', name2: 'b' },
        { id: 2, name1: 'c', name2: 'd' },
        { id: 3, name1: 'e', name2: 'a' }
        
    ],
    [
        { id: 4, name1: 't', name2: 'k' },
        { id: 5, name1: 'j', name2: 'h' },
        { id: 6, name1: 'z', name2: 'x' },
    ],
]

// function to determine if an object's name1 or name2
// property exists in an other object with a lower index
const isdupe = (obj, idx, arr) => 
  arr.findIndex(({ name1, name2 }, i) => [name1, name2].includes(obj.name1) || [name1, name2].includes(obj.name2)) != idx

const isnotdupe = (obj, idx, arr) => !isdupe(obj, idx, arr)

// iterate over the entries in the data array
for (let i = 0; i < data.length; i++) {
  // find the duplicates in this array
  let dupes = data[i].filter(isdupe)
  // if there are any, add (concat) them to the next array
  // (data[i+1] || []) will create a new entry if one doesn't exist
  if (dupes.length) data[i+1] = (data[i+1] || []).concat(dupes)
  // filter this array to only be non-duplicates
  data[i] = data[i].filter(isnotdupe)
}

console.log(data)

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

2 Comments

thanks, this works but I don't understand how, can you please add some comments in the for loop?
@doge47 please see my edit; if you still have questions feel free to ask...
0

I'm not quite sure what your asking for here, as you seem to grasp the concept, but i'm going to try and answer it, so appologies if its not a 100% fitting answer.

Every time that an item is added to the array, you could create a for statement that goes through every item on the array and checks if(item == new item) then it pushes it to array two, and repeats the checking process. If you are asking for the specific commands related to it, a great resorce I use is https://www.w3schools.com More specificaly for this question, https://www.w3schools.com/jsref/jsref_includes_array.asp

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.