首页 > 解决方案 > ([JavaScript) 我无法使用对象的整数键访问值

问题描述

如标题所述,我无法访问下面对象的值。

1: {id: 38, user_id: 3, review_id: 1, created_at: "2020-04-11T04:12:14.569Z", updated_at: "2020-04-11T04:12:14.569Z"}
2: {id: 24, user_id: 3, review_id: 2, created_at: "2020-04-11T03:25:16.589Z", updated_at: "2020-04-11T03:25:16.589Z"}
3: {id: 20, user_id: 3, review_id: 3, created_at: "2020-04-09T12:07:34.669Z", updated_at: "2020-04-09T12:07:34.669Z"}

该对象存储在 中likes,键位于 中reviewId。我确认这两个变量已使用 chrome 开发工具成功存储。 对象检查 reviewId 检查

我尝试了以下方法来访问这些值,但都没有奏效;

   if (likes) {
        console.log('reviewId is now ', reviewId);
        console.log('like of this reviewId is now ', likes['1']);
        console.log('like of this reviewId is now ', likes[`${reviewId}`]);
        console.log('like of this reviewId is now ', likes[reviewId]);
        console.log('like of this reviewId is now ', likes[String(reviewId)]);
        console.log('like of this reviewId is now ', likes.reviewId);
    }

如果我的理解是正确的,带有整数的键将被转换为字符串,所以我至少应该能够访问这些值likes['1']。除了我如何访问之外,可能还有一些问题,我猜?

如果您有任何建议,请告诉我。谢谢。

[补充] 请参考以下可能与此问题相关的代码。我正在使用 Redux + Hooks。

<./actions/like.js>
import axios from 'axios';
import * as actionTypes from './actionTypes';

export const setLike = likes => {
    return {
        type: actionTypes.SET_LIKE,
        likes: likes,
    };
};

export const fetchLike = (likes, userId, reviewId) => {
    return dispatch => {
        const url = `http://localhost:3001/users/${userId}/reviews/${reviewId}/likes/1`;
        axios
            .get(url)
            .then(response => {
                likes[reviewId] = response.data; 
                dispatch(setLike(likes));
            })
            .catch(error => {
                console.log(error);
            });
    }
};
<./reducer/like.js>
import * as actionTypes from '../actions/actionTypes';
import { updateObject } from './utility';

const initialState = {
    likes: {},
};

const setLike = (state, action) => {
    return updateObject(state, {
        likes: action.likes,
    });
};

const reducer = (state = initialState, action) => {
    switch (action.type) {
        case actionTypes.SET_LIKE:
            return setLike(state, action);
        default:
            return state;
    }
};

演示:

const likes = {
  1: {
    id: 38,
    user_id: 3,
    review_id: 1,
    created_at: "2020-04-11T04:12:14.569Z",
    updated_at: "2020-04-11T04:12:14.569Z"
  }
};
const reviewId = 1;

console.log('like of this reviewId is now ', likes['1']);
console.log('like of this reviewId is now ', likes[`${reviewId}`]);
console.log('like of this reviewId is now ', likes[reviewId]);
console.log('like of this reviewId is now ', likes[String(reviewId)]);
console.log('like of this reviewId is now ', likes.reviewId);
.as-console-wrapper {max-height: 100% !important;top: 0;}

标签: javascriptarraysobjectkey

解决方案


我相信您可能错过了钻取喜欢对象的所需层的步骤。看起来您已经从外层直接跳到每个编号对象的内层。

你有没有尝试过:

likes.1.reviewId

您的第一次尝试(见下文)应该返回一个对象,但不会给您我理解您正在寻找的 reviewId。

likes['1']

编辑:

我相信正确的语法应该是:

console.log(likes["1"].review_id);

推荐阅读