搜索 javascript 对象到 return 相关的 catId

Search javascript object to return the relevant catId

我有以下 javascript 数组,我正在尝试简单地 return catId 以获得 id - 例如,如果我搜索 id10000356我要return的值4。我应该怎么做?

categoryIds = [
    {
        "id": 10000282,
        "catId": 0
    },
    {
        "id": 10000340,
        "catId": 0
    },
    {
        "id": 10000341,
        "catId": 0
    },
    {
        "id": 10000334,
        "catId": 1
    },
    {
        "id": 10000333,
        "catId": 2
    },
    {
        "id": 10000336,
        "catId": 2
    },
    {
        "id": 10000337,
        "catId": 3
    },
    {
        "id": 10000356,
        "catId": 4
    }
]
categoryIds.filter(id => id == 10000356);

预期结果:

4 - the catId for `10000356` is the integer 4.
const result = categoryIds.find((data) => data?.id === 10000356); // beware result can be null
// use ?. to avoid error if data is null
// rembere that you have an array of object not of id ;)
console.log("the catId for 10000356 is the integer " + result?.catId);

如果您确定 ID 是唯一的,请使用查找,我认为这里就是这种情况。 find 将 return 您想要的数据,如果找不到 return null