青春有我
2023-04-27 16:32:19
我收到的 API 響應(yīng)為:[{"subject1": "English", "subject1": "Maths"}]我想將值 (English和Maths) 存儲(chǔ)到一個(gè)沒(méi)有鍵的數(shù)組中,例如:subject = ["English", "Maths"]
3 回答

小怪獸愛(ài)吃肉
TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊
也許這可以解決您的問(wèn)題:
// if you have variable number of subject
let res = [{"subject1":"English", "subject2":"Maths"}]
let subjects = []
for(prop in res[0]){
subjects.push(res[0][prop])
}
console.log(subjects)

FFIVE
TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
給定一個(gè)對(duì)象數(shù)組:
[{"subject1":"English"}, {"subject1":"Maths"}]
用這個(gè):
let res = foo.map(e => e.subject1)
console.log(res) // prints ["English", "Maths"]

四季花海
TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
這能解決您的問(wèn)題嗎?
subject = []; subject.push(Object.values(response));
添加回答
舉報(bào)
0/150
提交
取消