【发布时间】:2020-12-11 04:49:17
【问题描述】:
您好,我对 JSON 和 API 非常陌生,并且正在研究一个我在数组中获取价值的 API。
{
id: 0,
text: "one"
}
{
id: 1,
text: "two"
}
{
id: 2,
text: "three"
}
我只想要一个数组的值,其中id=0 并且我不知道如何在 JSON 中对数组进行排序来实现这一点,我使用 ajax 来获取这个值
async function getDataFromId() {
let url = 'API_URL';
let Qid = 1;
let result;
try {
result = await jQuery.ajax({
url: url,
type: 'GET',
contentType: 'application/json',
});
result = result.sort((a, b) => a.id- b.id);
console.log(result)
}catch (error) {
console.error(error);
}
}
【问题讨论】:
-
如果只想要
id=0的值,为什么还要排序呢?过滤还不够吗? -
@Turtlean 很抱歉,这是非常菜鸟的事情,但是如何过滤?
-
试试这个:
var new_array = result.filter(function(i){return i.id == 0})
标签: javascript jquery arrays json