我收到的 API 响应为:[{"subject1": "English", "subject1": "Maths"}]我想将值 (English和Maths) 存储到一个没有键的数组中,例如:subject = ["English", "Maths"]
3 回答

小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
也许这可以解决您的问题:
// 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贡献1797条经验 获得超6个赞
给定一个对象数组:
[{"subject1":"English"}, {"subject1":"Maths"}]
用这个:
let res = foo.map(e => e.subject1)
console.log(res) // prints ["English", "Maths"]
添加回答
举报
0/150
提交
取消