我提出了一个HTTP请求,要求从表中提取一列数据。在提取此数据之后,尝试在对象内部遍历对象和数组时遇到问题。我尝试使用Array.from()
和split()
将对象转换成数组,但是返回对象内部的每个字符,并用空格分隔。我如何遍历主对象内部的对象数组以获得每个名为“值”的键字段的值?
这是我随请求拉出的对象:
0: "{"
data ":[{"
8 ":{"
value ":"
644 Lucon Road Back Warehouse, Schwenksville, North Dakota 58963 "}},{"
8 ":{"
value ":"
5689 Henkels Lane Suite 504, Annapolis Junction, New Jersey 58963 "}},{"
8 ":{"
value ":"
2563 Dickerson Pike Suite 105, Nashville, North Dakota 58963 "}},{"
8 ":{"
value ":"
123 Country Lane, Prosperity, North Dakota 58963 "}},{"
8 ":{"
value ":"
896 Cember Way Suite A, Summerville, North Carolina 58963 "}},{"
8 ":{"
value ":"
1125 NW 97 th Avenue, Doral, Texas 58963 "}},{"
8 ":{"
value ":"
896 Hill Avenue Suite C, Toledo, Alabama 58963 "}},{"
8 ":{"
value ":"
2101 Pleasant Valley Lane Suite B, Arlington, Texas 58963 "}},{"
8 ":{"
value ":"
1000 S Marietta Parkway SE Suite 105, Marietta, Timbuktu 35896 "}},{"
8 ":{"
value ":"
1258 103 rd Street Suite 127, Jacksonville, Texas 35896 "}},{"
8 ":{"
value ":"
9856 Corporate Drive North Suite 201, Mobile, Maryland 58963 "}},{"
8 ":{"
value ":"
9563 Leah Drive Unit 1, Hillsborough, North Dakota 25896 "}},{"
8 ":{"
value ":"
5689 Brookshire Road Unit D, Greer, North Dakota 58963 "}},{"
8 ":{"
value ":"
1587 Kessler Avenue Suite 301, Schulenburg, Arizona 58963 "}},{"
8 ":{"
value ":"
fields ":[{"
id ":8,"
label ":"
Address ","
type ":"
address "}],"
metadata ":{"
numFields ":1,"
numRecords ":15,"
skip ":0,"
totalRecords ":15}}"
这是我的要求:
const headers = {
'QB-Realm-Hostname': 'xxxxx.quickbase.com',
'Authorization': 'QB-USER-TOKEN xxxxxxxxxxxxxxxxxxxxxxxxxxx',
'Content-Type': 'application/json'
}
const body = {
"from": "bn6rb5ll9",
"select": [8],
"options": {
"skip": 0,
"top": 0
}
}
const xmlHttp = new XMLHttpRequest();
xmlHttp.open('POST', 'https://api.quickbase.com/v1/records/query', true);
for (const key in headers) {
xmlHttp.setRequestHeader(key, headers[key]);
}
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState === XMLHttpRequest.DONE) {
let jsonObject = xmlHttp.responseText.split();
console.log(jsonObject);
// for (let i = 0; i <= jsonObject.length; i++) {
// console.log((jsonObject[i]));
// }
// const propValues = Object.values(xmlHttp.responseText);
// console.log(propValues);
}
};
xmlHttp.send(JSON.stringify(body));
这是我使用Array.from(xmlHttpResponseText)
(1242) ["{", """, "d", "a", "t", "a", """, ":", "[", "{", """, "8", """, ":", "{", """, "v", "a", "l", "u", "e", """, ":", """, "2", "0", "3", "3", " ", "L", "u", "c", "o", "n", " ", "R", "o", "a", "d", " ", "B", "a", "c", "k", " ", "W", "a", "r", "e", "h", "o", "u", "s", "e", ",", " ", "S", "c", "h", "w", "e", "n", "k", "s", "v", "i", "l", "l", "e", ",", " ", "P", "e", "n", "n", "s", "y", "l", "v", "a", "n", "i", "a", " ", "1", "9", "4", "7", "3", """, "}", "}", ",", "{", """, "8", """, ":", "{", """, …]
我使用了JSON.parse(xmlHttp.responseText),并且确实设法获得了typeof数组,但是我看不到任何可用于该数组的数组方法。
答案 0 :(得分:1)
如前所述,您的有效载荷不是有效格式,因此我将以从Array.from(xmlHttpResponseText)获得的文本作为基础。
首先,如果您的有效载荷是JSON,则必须将其作为json处理,而不是文本行:
let jsonObject = JSON.parse(xmlHttp.responseText);
第二,我想从数据中提取数据是从结构中提取地址。我们将使用Array.map遍历数据的每个条目,并提取属性8.value:
const addresses=jsonObject.data.map(e=>e["8"].value);
然后,您将获得一个字符串数组,这些字符串是您的地址,可以在控制台上登录,或者使用以下方法进行任何操作:
console.log(addresses);