javascript - 需要一种方法从对象数组中随机选取一个值而不遍历父对象

标签 javascript arrays

我对 JavaScript 还很陌生,需要帮助来完成我想要完成的任务。

我有一个像这样的对象数组:

    const data =
{
    "Total_packages": {
        "package1": {
            "tags": [
                "kj21",
                "j1",
                "sj2",
                "z1"
            ],
            "expectedResponse": [
                {
                    "firstName": "Name",
                    "lastName": "lastName",
                    "purchase": [
                        {
                            "title": "title",
                            "category": [
                                "a",
                                "b",
                                "c"
                            ]
                        }
                    ]
                }
            ]
        },
        "package2": {
            "tags": [
                "s2",
                "dsd3",
                "mhg",
                "sz7"
            ],
            "expectedResponse": [
                {
                    "firstName": "Name1",
                    "lastName": "lastName1",
                    "purchase": [
                        {
                            "title": "title1",
                            "category": [
                                "a1",
                                "b1",
                                "c1"
                            ]
                        }
                    ]
                }
            ]
        },
        "package3": {
            "tags": [
                "s21",
                "dsd31",
                "mhg1",
                "sz71"
            ],
            "expectedResponse": [
                {
                    "firstName": "Name2",
                    "lastName": "lastName2",
                    "purchase": [
                        {
                            "title": "title2",
                            "category": [
                                "a2",
                                "b2",
                                "c2"
                            ]
                        }
                    ]
                }
            ]
        },
        "package4": {
            "tags": [
                "s22",
                "dsd32",
                "mhg2",
                "sz72"
            ],
            "expectedResponse": [
                {
                    "firstName": "Name3",
                    "lastName": "lastName3",
                    "purchase": [
                        {
                            "title": "title3",
                            "category": [
                                "a3",
                                "b3",
                                "c3"
                            ]
                        }
                    ]
                }
            ]
        },
        "package5": {
            "tags": [
                "s22",
                "dsd32",
                "mhg2",
                "sz72"
            ],
            "expectedResponse": [
                {
                    "firstName": "Name4",
                    "lastName": "lastName4",
                    "purchase": [
                        {
                            "title": "title4",
                            "category": [
                                "a4",
                                "b4",
                                "c4"
                            ]
                        }
                    ]
                }
            ]
        }
    }
}

    var arrRand = genNum(data, 3);
    console.log(arrRand);

               function genNum(data, loop='') {
                    var list = [];
                    var arrayOfTags = Object.entries(data.Total_packages).reduce((acc, [k, v]) => {
                        if (v.tags) acc = acc.concat(v.tags.map(t => ({tag: t, response: v.expectedResponse})));
                        return acc;
                    }, []);
                    for (var i = 0; i < loop; i++){
                      var randomIndex = Math.floor(Math.random() * arrayOfTags.length);
                    var randomTag = arrayOfTags[randomIndex];
                    list.push(randomTag);
                    }
                    return list;

        }

然后,我通过执行 arrRand[0].tag 和 arrRand[0].response 等操作来访问所需的值。

我经常从以下方法中得到重复的响应,这就会出现问题:

[
  {
    "tag": "s22",
    "response": [
      {
        "firstName": "Name4",
        "lastName": "lastName4",
        "purchase": [
          {
            "title": "title4",
            "category": [
              "a4",
              "b4",
              "c4"
            ]
          }
        ]
      }
    ]
  },
  {
    "tag": "dsd31",
    "response": [
      {
        "firstName": "Name2",
        "lastName": "lastName2",
        "purchase": [
          {
            "title": "title2",
            "category": [
              "a2",
              "b2",
              "c2"
            ]
          }
        ]
      }
    ]
  },
  {
    "tag": "dsd31",
    "response": [
      {
        "firstName": "Name2",
        "lastName": "lastName2",
        "purchase": [
          {
            "title": "title2",
            "category": [
              "a2",
              "b2",
              "c2"
            ]
          }
        ]
      }
    ]
  }
]

我的目标是发送带有上面随机“标签”值的 API 请求,然后将我从调用中获得的响应与数据的预期响应部分进行匹配。

我最初的想法是做类似的事情:

data.Total_packages.tags[Math.floor(Math.random() * data.Total_packages.tags.length)];

但是,我无法在不遍历其父级(即“package1”或“package2”)的情况下调用“tags”,因此它不再是随机的。

我知道可能有一种非常简单的方法可以做到这一点,但我没有得到。任何建议将不胜感激。

最佳答案

您可以从各种标签元素构建一个数组,并使用它来执行随机操作。

const data =
{
    "Total_packages": {
        "package1": {
            "tags": [
                "kj21",
                "j1",
                "sj2",
                "z1"
            ],
            "expectedResponse": [
                {
                    "firstName": "Name",
                    "lastName": "lastName",
                    "purchase": [
                        {
                            "title": "title",
                            "category": [
                                "a",
                                "b",
                                "c"
                            ]
                        }
                    ]
                }
            ]
        },
        "package2": {
            "tags": [
                "s2",
                "dsd3",
                "mhg",
                "sz7"
            ],
            "expectedResponse": [
                {
                    "firstName": "Name1",
                    "lastName": "lastName1",
                    "purchase": [
                        {
                            "title": "title1",
                            "category": [
                                "a1",
                                "b1",
                                "c1"
                            ]
                        }
                    ]
                }
            ]
        }
    }
}

const arrayOfTags = Object.entries(data.Total_packages).reduce((acc, [k, v]) => {
  if (v.tags) acc = acc.concat(v.tags.map(t => ({tag: t, response: v.expectedResponse})));
  return acc;
}, []);

const randomIndex = Math.floor(Math.random() * arrayOfTags.length);

const randomTag = arrayOfTags[randomIndex];

console.log(randomTag.tag, randomTag.response);

关于javascript - 需要一种方法从对象数组中随机选取一个值而不遍历父对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73544807/

相关文章:

javascript - 递归Function.prototype.apply概念理解

c - C程序中的"Array subscript is not an integer"

php - 内爆二维数组中的数据

javascript - 我想在单击更新按钮时自动获得状态空间

javascript - 无法获得适合 SVG Box 的 SVG 路径

javascript - 检查对象数组中的所有值是否都等于 false 并在 React 中返回 true

javascript - 如何查找键值对数组中最大的数字 JavaScript

java - 我正在尝试合并两个整数字符串,但我正在使用的程序 Java 博士不允许我使用 ArrayUtils?

javascript - jqplot:在两条线之间填充颜色

javascript - 从 phantomjs 网络服务器返回二进制结果