这是到目前为止我尝试过的方法,但我只能得到1个值。
Object.keys(yelpResults).map((key) => {
return setRestaurantIndexes(restaurantIndexes.concat(key));
});
答案 0 :(得分:-1)
如果有帮助,您可以参考此示例。 Map函数可以在array上执行,因此首先我们需要将对象转换为array,然后对其执行操作。
const details={
name:'John',
lName:'lee',
country:'US'
}
const getIndex = Object.keys(details).map((item , i)=>{
return i;
});
console.log(getIndex);
答案 1 :(得分:-1)
单独使用函数Object.keys
可以解决问题,它返回一个数组,其中包含对象的属性和方法的名称。
const data = {
name: 'John Doe',
age: 50,
country: 'NZ'
}
const propertyNames = Object.keys(data)
console.log(propertyNames);
// prints: ['name', 'age', 'country']
答案 2 :(得分:-1)
首先创建您的对象。
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($i=0; $i <= $col_count; $i++) {
echo "<tr>\n";
for ($j=0; $j <= $row_count; $j++) {
$p = $data2[$i][$j];
echo "<td>$p</td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
然后声明一个数组,您将在其中保存索引值
const result = ["Apple", "Orange", "Plum"];
然后遍历在步骤1中创建的对象。
let newArray = []
控制台使用已保存的索引值记录新数组。
//loop the results
for(var i=0, l=result.length; i < l; i++){
//push the object's index to the array
newArray.push(i);
}