如何从 JavaScript 中的对象获取密钥?
How do I get the keys from an object in JavaScript?
我在处理由 GraphQl 调用 dynamoDB 返回的对象时遇到困难。我给每个嵌套对象一个数字作为键。这是我尝试使用 Object.keys()
创建键数组的数字
const groupsContext = {"01":{"Status":"Active","Name":"Group 1"},"02":{"Status":"Active","Name":"Group 2"}}
let groupKeys = Object.keys(groupsContext)
console.log(groupKeys)
这是控制台输出:
Array(2897) [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", … ]
我已经在 codepen 上试过了,它确实有效,所以我不确定有什么不同。我唯一能想到的是,我在创建应用程序上下文和尝试从上下文创建键之间遗漏了一些东西。
这是我用来创建上下文的代码:
//上下文文件
import { useContext, createContext } from "react";
export const GroupsContext = createContext(null);
export function useGroupsContext() {
return useContext(GroupsContext);
}
//应用
import React, { useState, useEffect } from "react";
import { GroupsContext } from "./libs/context/groupsLib";
function App() {
const [groupsContext, setGroupsContext] = useState(false);
useEffect(() => {
onLoad();
}, []);
async function onLoad() {
let apiGroups
try {
apiGroups = await API.graphql(graphqlOperation(queries.getQuery, { PK: "Status", SK: "Test" }));
setGroupsContext(apiGroups.data.getQuery.FullGroups)
} catch (e) {
if (e !== "Error") {
onError(e);
}
}
}
return (
<div>
<GroupsContext.Provider value={{ groupsContext, setGroupsContext }}>
<Routing />
</GroupsContext.Provider>
</div>
)
);
}
export default App;
//获取组
import { useGroupsContext } from "../../libs/context/groupsLib";
export default function ActiveGroups() {
const { groupsContext } = useGroupsContext()
let groupKeys = Object.keys(groupsContext)
console.log(groupKeys)
如您所见,数组的长度应该只有两个。如何创建数组以仅显示数字键?
感谢 Nick P 对可枚举属性的思考。我已将代码修改为:
let groupKeys = []
for (const key in groupsContext) {
groupKeys.push(`${key}`);
}
console.log(groupKeys)
我在处理由 GraphQl 调用 dynamoDB 返回的对象时遇到困难。我给每个嵌套对象一个数字作为键。这是我尝试使用 Object.keys()
创建键数组的数字const groupsContext = {"01":{"Status":"Active","Name":"Group 1"},"02":{"Status":"Active","Name":"Group 2"}}
let groupKeys = Object.keys(groupsContext)
console.log(groupKeys)
这是控制台输出:
Array(2897) [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", … ]
我已经在 codepen 上试过了,它确实有效,所以我不确定有什么不同。我唯一能想到的是,我在创建应用程序上下文和尝试从上下文创建键之间遗漏了一些东西。
这是我用来创建上下文的代码:
//上下文文件
import { useContext, createContext } from "react";
export const GroupsContext = createContext(null);
export function useGroupsContext() {
return useContext(GroupsContext);
}
//应用
import React, { useState, useEffect } from "react";
import { GroupsContext } from "./libs/context/groupsLib";
function App() {
const [groupsContext, setGroupsContext] = useState(false);
useEffect(() => {
onLoad();
}, []);
async function onLoad() {
let apiGroups
try {
apiGroups = await API.graphql(graphqlOperation(queries.getQuery, { PK: "Status", SK: "Test" }));
setGroupsContext(apiGroups.data.getQuery.FullGroups)
} catch (e) {
if (e !== "Error") {
onError(e);
}
}
}
return (
<div>
<GroupsContext.Provider value={{ groupsContext, setGroupsContext }}>
<Routing />
</GroupsContext.Provider>
</div>
)
);
}
export default App;
//获取组
import { useGroupsContext } from "../../libs/context/groupsLib";
export default function ActiveGroups() {
const { groupsContext } = useGroupsContext()
let groupKeys = Object.keys(groupsContext)
console.log(groupKeys)
如您所见,数组的长度应该只有两个。如何创建数组以仅显示数字键?
感谢 Nick P 对可枚举属性的思考。我已将代码修改为:
let groupKeys = []
for (const key in groupsContext) {
groupKeys.push(`${key}`);
}
console.log(groupKeys)