Javascript 如何创建所有阵列组合?

Javascript 如何创建所有阵列组合?,javascript,arrays,Javascript,Arrays,如何转换无重复项的«多对多»数组?我们需要阵列之间所有可能的匹配。请记住,数据可以重复,因此应避免重复记录 我的数据: [ { “id”:[ “堆栈溢出” ], “你好”:[ “你好”, “你好” ], “世界”:[ “世界1号”, “世界2” ] }, { “id”:[ “stackexchange” ], “我的”:[ “my1” ], “世界”:[ “世界1号”, “世界3” ] }, { “id”:[ “堆栈溢出” ], “你好”:[ “你好” ], “世界”:[ “世界1” ] } ]

如何转换无重复项的«多对多»数组?我们需要阵列之间所有可能的匹配。请记住,数据可以重复,因此应避免重复记录

我的数据:

[
{
“id”:[
“堆栈溢出”
],
“你好”:[
“你好”,
“你好”
],
“世界”:[
“世界1号”,
“世界2”
]
}, {
“id”:[
“stackexchange”
],
“我的”:[
“my1”
],
“世界”:[
“世界1号”,
“世界3”
]
}, {
“id”:[
“堆栈溢出”
],
“你好”:[
“你好”
],
“世界”:[
“世界1”
]
}
]
需要所有数组元素的结果:

[
{
“id”:“stackoverflow”,
“你好”:“你好”,
“世界”:“世界1”
}, {
“id”:“stackoverflow”,
“你好”:“你好”,
“世界”:“世界2”
}, {
“id”:“stackoverflow”,
“你好”:“你好”,
“世界”:“世界1”
}, {
“id”:“stackoverflow”,
“你好”:“你好”,
“世界”:“世界2”
}, {
“id”:“stackexchange”,
“my”:“my1”,
“世界”:“世界1”
}, {
“id”:“stackexchange”,
“my”:“my1”,
“世界”:“世界3”
}
]

感谢您的回复。

您可以将JSON解析为一个类,并使用HashSet contains方法避免重复数据

像这样的班级

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;
    }
}
解析

Item item = gson.fromJson(jsonString, Item.class);
您可以在此处阅读有关解析的更多信息,不要忘记导入渐变:

此数据

  {
    "id": "stackoverflow",
    "hello": "hello1",
    "world": "world1"
  }
可以像这样访问吗

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

当您在接收json数据时循环数据时,您可以删除重复的数据,例如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());
}
HashSet allData=new HashSet();

对于(inti=0;i,您可以将JSON解析为一个类,并使用hashsetcontains方法避免重复数据

像这样的班级

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;
    }
}
解析

Item item = gson.fromJson(jsonString, Item.class);
您可以在此处阅读有关解析的更多信息,不要忘记导入渐变:

此数据

  {
    "id": "stackoverflow",
    "hello": "hello1",
    "world": "world1"
  }
可以像这样访问吗

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

当您在接收json数据时循环数据时,您可以删除重复的数据,例如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());
}
HashSet allData=new HashSet();

对于(int i=0;i,此函数将根据指定的属性创建所有可能对象的组合:

const数据=[
{
“id”:[
“堆栈溢出”
],
“你好”:[
“你好”,
“你好”
],
“世界”:[
“世界1号”,
“世界2”
]
}, {
“id”:[
“stackexchange”
],
“我的”:[
“my1”
],
“世界”:[
“世界1号”,
“世界3”
]
}, {
“id”:[
“堆栈溢出”
],
“你好”:[
“你好”
],
“世界”:[
“世界1”
]
}
]
const res=data.map(d=>{
常量唯一组合=对象值(d)
.map(v=>v.length)
.减少((a,b)=>a*b)
常量innerRes=[…数组(唯一组合)].map(=>({}))
常量键=对象键(d)
const keyInfo=[]
for(设i=keys.length-1;i>-1;--i){
keyInfo.unshift({
k:键[i],
v:d[键[i]],
切换速率:keyInfo.length?keyInfo.map(ki=>ki.v.length).reduce((a,b)=>(a*b)):1,
})
}
内部资源(o,i)=>{
keyInfo.forEach(ki=>{
设index=Math.max(0,Math.ceil(i/ki.switchRate))
而(索引>=ki.v.length){
指数-=基伏长度
}
o[ki.k]=ki.v[index]
})
})
返回内部
}).单位(1)

console.log(res)
此函数将根据指定的属性创建所有可能对象的组合:

const数据=[
{
“id”:[
“堆栈溢出”
],
“你好”:[
“你好”,
“你好”
],
“世界”:[
“世界1号”,
“世界2”
]
}, {
“id”:[
“stackexchange”
],
“我的”:[
“my1”
],
“世界”:[
“世界1号”,
“世界3”
]
}, {
“id”:[
“堆栈溢出”
],
“你好”:[
“你好”
],
“世界”:[
“世界1”
]
}
]
const res=data.map(d=>{
常量唯一组合=对象值(d)
.map(v=>v.length)
.减少((a,b)=>a*b)
常量innerRes=[…数组(唯一组合)].map(=>({}))
常量键=对象键(d)
const keyInfo=[]
for(设i=keys.length-1;i>-1;--i){
keyInfo.unshift({
k:键[i],
v:d[键[i]],
切换速率:keyInfo.length?keyInfo.map(ki=>ki.v.length).reduce((a,b)=>(a*b)):1,
})
}
内部资源(o,i)=>{
keyInfo.forEach(ki=>{
设index=Math.max(0,Math.ceil(i/ki.switchRate))
而(索引>=ki.v.length){
指数-=基伏长度
}
o[ki.k]=ki.v[index]
})
})
返回内部
}).单位(1)

console.log(res)
你能展示一下你的尝试吗?你需要某种循环-虽然数组映射/减少和其他数组方法可能会使工作更容易…很好luck@JohnnyZabala就这样一个多维数组中的数据,我想把它转换成一种更方便的工作格式。@lockwise我理解你想要什么,但正如你在这里看到的:,它是导入以显示您自己的研究和解决问题的努力。在StackOverflow中提问应该是您的最后手段。对不起!我不知道如何设置问题的格式。您可以展示您尝试过的内容吗?您需要某种循环-尽管数组映射/减少和其他数组方法可能会使工作更轻松…很好luck@JohnnyZabala就是这样一个多维数组中的数据,我想把它转换成一种更方便的工作格式。@lockwise我理解您想要什么,但您可以在这里阅读: