gpt4 book ai didi

javascript - 过滤数组,获取列表

转载 作者:行者123 更新时间:2023-12-01 08:33:31 25 4
gpt4 key购买 nike

我有一个像这样的数组:

["q1":[{"key_api":86,"question_api":"q1","answer_api":"a1"},{"key_api":87,"question_api":"q1","answer_api":"a2"},{"key_api":88,"question_api":"q1","answer_api":"a3"},{"key_api":89,"question_api":"q1","answer_api":"a4"}],
"q2":[{"key_api":90,"question_api":"q2","answer_api":"a5"},{"key_api":91,"question_api":"q2","answer_api":"a7"}{"key_api":92,"question_api":"q2","answer_api":"a9"}],
"q3":[{"key_api":93,"question_api":"q3","answer_api":"a1"},{"key_api":94,"question_api":"q3","answer_api":"a2"},{"key_api":95,"question_api":"q3","answer_api":"a3"},{"key_api":96,"question_api":"q3","answer_api":"a4"}],
"q4":[{"key_api":97,"question_api":"q4","answer_api":"a10"},{"key_api":98,"question_api":"q4","answer_api":"a11"},{"key_api":99,"question_api":"q4","answer_api":"a12"},{"key_api":100,"question_api":"q4","answer_api":"a13"}]]

另外,我有这个字符串:

"q4"

我想过滤数组,获取此列表(实际上,第三个问题的可能答案):

["a10", "a11", "a12", "a13"]

如果可能的话,我更喜欢原生js(但如果涉及大量代码我可以考虑库)。我现在使用 js 一星期了:像这样的操作在 Python 中感觉很自然,但在这里我遇到了麻烦。非常感谢!

最佳答案

假设您提供的初始 JSON 实际上是一个由数组属性组成的对象(因为 JSON 数组不能有属性),您只需在其中之一上使用 Array.prototype.map 即可对象的属性:

   const dataObject = {
"q1": [{ "key_api": 86, "question_api": "q1", "answer_api": "a1" }, { "key_api": 87, "question_api": "q1", "answer_api": "a2" }, { "key_api": 88, "question_api": "q1", "answer_api": "a3" }, { "key_api": 89, "question_api": "q1", "answer_api": "a4" }],
"q2": [{ "key_api": 90, "question_api": "q2", "answer_api": "a5" }, { "key_api": 91, "question_api": "q2", "answer_api": "a7" }, { "key_api": 92, "question_api": "q2", "answer_api": "a9" }],
"q3": [{ "key_api": 93, "question_api": "q3", "answer_api": "a1" }, { "key_api": 94, "question_api": "q3", "answer_api": "a2" }, { "key_api": 95, "question_api": "q3", "answer_api": "a3" }, { "key_api": 96, "question_api": "q3", "answer_api": "a4" }],
"q4": [{ "key_api": 97, "question_api": "q4", "answer_api": "a10" }, { "key_api": 98, "question_api": "q4", "answer_api": "a11" }, { "key_api": 99, "question_api": "q4", "answer_api": "a12" }, { "key_api": 100, "question_api": "q4", "answer_api": "a13" }]
};
console.log(dataObject['q4'].map(item => item.answer_api));

如果您想对对象中的所有条目/键执行此操作,您可以通过迭代返回值将其与 Object.keys 结合起来,如下所示:

const dataObject = {
"q1": [{ "key_api": 86, "question_api": "q1", "answer_api": "a1" }, { "key_api": 87, "question_api": "q1", "answer_api": "a2" }, { "key_api": 88, "question_api": "q1", "answer_api": "a3" }, { "key_api": 89, "question_api": "q1", "answer_api": "a4" }],
"q2": [{ "key_api": 90, "question_api": "q2", "answer_api": "a5" }, { "key_api": 91, "question_api": "q2", "answer_api": "a7" }, { "key_api": 92, "question_api": "q2", "answer_api": "a9" }],
"q3": [{ "key_api": 93, "question_api": "q3", "answer_api": "a1" }, { "key_api": 94, "question_api": "q3", "answer_api": "a2" }, { "key_api": 95, "question_api": "q3", "answer_api": "a3" }, { "key_api": 96, "question_api": "q3", "answer_api": "a4" }],
"q4": [{ "key_api": 97, "question_api": "q4", "answer_api": "a10" }, { "key_api": 98, "question_api": "q4", "answer_api": "a11" }, { "key_api": 99, "question_api": "q4", "answer_api": "a12" }, { "key_api": 100, "question_api": "q4", "answer_api": "a13" }]
};
Object.keys(dataObject).forEach(key => {
console.log(dataObject[key].map(item => item.answer_api));
})

关于javascript - 过滤数组,获取列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59652186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com