3 回答

TA贡献1840条经验 获得超5个赞
你可以做:
const response = {
"statusCode": 200,
"status": "success",
"data": [
[{
"city": "Alexandria",
"country": "Egypt",
},
{
"city": "Alexandria",
"country": "Egypt",
},
]
]
};
const getData = data => data[0];
console.log(getData(response.data));

TA贡献1836条经验 获得超13个赞
getData = function(data){
arr = data.data[0];
new_data = []
for(var item in arr){
new_data.push(arr[item])
}
}
希望这会帮助你。

TA贡献1802条经验 获得超10个赞
首先,您data[0]是一个数组,然后Object.keys(array)将返回indexthat的数组array。前任:
array= [{x: 1}, {x: 2}, {x: 3}]
Object.keys(array) // ['0', '1', '2']
因此,您推送到return数组的只是与您显示的索引相同的索引。
其次,您应该使用不同的变量名以避免误解。在这种情况下,是data可变的。
我更新了功能
const object = {"statusCode": 200,"status": "success","data": [[{"city": "Alexandria","country": "Egypt",},{"city": "Alexandria","country": "Egypt",},]]}
getData = function (arr) {
data = []
arr[0].forEach(function (key) {
data.push(key);
});
return data
}
console.log(getData(object.data))
添加回答
举报