0

How to convert an array of «many to many» no duplicate entries? We need all possible matches between arrays. Keep in mind that data can be repeated, so duplication of records should be avoided.

My data:

[
  {
    "id": [
      "stackoverflow"
    ],
    "hello": [
      "hello1",
      "hello2"
    ],
    "world": [
      "world1",
      "world2"
    ]
  }, {
    "id": [
      "stackexchange"
    ],
    "my": [
      "my1"
    ],
    "world": [
      "world1",
      "world3"
    ]
  }, {
    "id": [
      "stackoverflow"
    ],
    "hello": [
      "hello1"
    ],
    "world": [
      "world1"
    ]
  }
]

Need result all array elements:

[
  {
    "id": "stackoverflow",
    "hello": "hello1",
    "world": "world1"
  }, {
    "id": "stackoverflow",
    "hello": "hello1",
    "world": "world2"
  }, {
    "id": "stackoverflow",
    "hello": "hello2",
    "world": "world1"
  }, {
    "id": "stackoverflow",
    "hello": "hello2",
    "world": "world2"
  }, {
    "id": "stackexchange",
    "my": "my1",
    "world": "world1"
  }, {
    "id": "stackexchange",
    "my": "my1",
    "world": "world3"
  }
]

Thank you for your responses.

lockwise
  • 53
  • 1
  • 5
  • 1
    Can you show what you have tried? – Johnny Zabala Feb 14 '20 at 02:36
  • you'd need some sort of loop - though array map/reduce and other array methods may make the job easier ... good luck – Jaromanda X Feb 14 '20 at 02:37
  • @JohnnyZabala Just data in such a multidimensional array, I would like to bring it into a more convenient format for work. – lockwise Feb 14 '20 at 02:51
  • @lockwise I understand what you want but as you can read here: https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users, it is import for you to show your research and efforts to solve the problem on you own. Asking a question in StackOverflow should be your last resort. – Johnny Zabala Feb 14 '20 at 03:02
  • Excuse me! I did not know how to format the question. – lockwise Feb 14 '20 at 03:05

2 Answers2

1

You can parse your JSON into a class and use HashSet contains method to avoid repeated data.

A class like

class Item{
    String id;
    String hello;
    String world;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getHello() {
        return hello;
    }

    public void setHello(String hello) {
        this.hello = hello;
    }

    public String getWorld() {
        return world;
    }

    public void setWorld(String world) {
        this.world = world;
    }
}

To parse

Item item = gson.fromJson(jsonString, Item.class);

You can read more about parsing here, don't forget to import the gradle: How to parse JSON in Java

This data

  {
    "id": "stackoverflow",
    "hello": "hello1",
    "world": "world1"
  }

Can be access like

Item.get(0).getId(); //stackoverflow
Item.get(0).getHello(); //hello1
Item.get(0).getWorld(); //world1

When you loop through your data when you receive json data you can remove repeated e.g Id

HashSet<String> allData = new HashSet<String>(); 

for(int i = 0; i<Item.size(); i++){
  if(allData .contains(Item.get(i).getId()) continue;
  else allDData.add(Item.get(i).getId());
}

This is just pseudocode-ish Java code. Hope this helps!

unobatbayar
  • 1,223
  • 1
  • 10
  • 24
1

This function will create all combinations of possible objects from the properties specified:

const data = [
  {
    "id": [
      "stackoverflow"
    ],
    "hello": [
      "hello1",
      "hello2"
    ],
    "world": [
      "world1",
      "world2"
    ]
  }, {
    "id": [
      "stackexchange"
    ],
    "my": [
      "my1"
    ],
    "world": [
      "world1",
      "world3"
    ]
  }, {
    "id": [
      "stackoverflow"
    ],
    "hello": [
      "hello1"
    ],
    "world": [
      "world1"
    ]
  }
]

    const res = data.map(d => {
        const uniqueCombinations = Object.values(d)
            .map(v => v.length)
            .reduce((a, b) => a * b)
    
        const innerRes = [...Array(uniqueCombinations)].map(_ => ({}))
        const keys = Object.keys(d)
    
        const keyInfo = []
    
        for (let i = keys.length - 1; i > -1; --i) {
            keyInfo.unshift({
                k: keys[i],
                v: d[keys[i]],
                switchRate: keyInfo.length ? keyInfo.map(ki => ki.v.length).reduce((a, b) => (a * b)) : 1,
            })
        }
    
        innerRes.forEach((o, i) => {
            keyInfo.forEach(ki => {
                let index = Math.max(0, Math.ceil(i / ki.switchRate))
    
                while (index >= ki.v.length) {
                    index -= ki.v.length
                }
    
                o[ki.k] = ki.v[index]
            })
        })
    
        return innerRes
    }).flat(1)
    
    console.log(res)

Now all you have to do is filter the duplicate objects.

JMadelaine
  • 2,859
  • 1
  • 12
  • 17
  • Thank you very much for your reply. The solution with the double removal will be here: https://jsfiddle.net/cg1s5aj2/ – lockwise Feb 14 '20 at 13:26