【问题标题】:Why i cant get the object value instead of names: [ [Object], [Object], [Object], [Object] ]?为什么我无法获取对象值而不是名称:[[Object]、[Object]、[Object]、[Object]]?
【发布时间】:2021-04-15 03:00:37
【问题描述】:
const Data = [
    {
        id: 1,
        name: "jack"
    },
    {
        id: 1,
        name: "george"
    },
    {
        id: 1,
        name: "morries"
    },
    {
        id: 1,
        name: "kok"
    },
    {
        id: 2,
        name: "abdo"
    },
    {
        id: 2,
        name: "ezz"
    },
    {
        id: 3,
        name: "mostafa"
    },
    {
        id: 3,
        name: "tox"
    },
    {
        id: 4,
        name: "hamo"
    }]

var new_array = [];

for (var i = 0; i < Data.length; i++) {
    var data_id = Data[i].id;
    var data_name = Data[i];
    var ref_index = add(new_array, data_id);
    new_array[ref_index].names.push({ data: data_name });

}

console.log(new_array);

function add(data_array, id_value) {
    var index = data_array.findIndex(x => x.id === id_value);
    if (index === -1) {
        data_array.push({ id: id_value, names: [] });
        index = data_array.findIndex(x => x.id === id_value);
    }
    return index;
}

为什么我的回复是这样的,我需要查看我的对象的价值,我不知道缺少什么!

[
    { id: 1, names: [[Object], [Object], [Object], [Object]] },
    { id: 2, names: [[Object], [Object]] },
    { id: 3, names: [[Object], [Object]] },
    { id: 4, names: [[Object]] }
]

我需要查看对象本身,所以我尝试了代码,但它的响应类似于上面的代码?

【问题讨论】:

  • 不要在您的帖子中添加垃圾文本来绕过要求您解释更多问题的要求 - 相反,请更详细地解释问题,例如作为您尝试过的哪种调试不起作用。查找如何创建 minimal reproducible example 也可能会有所帮助 - 考虑删除与问题不直接相关的代码。

标签: javascript reactjs react-native redux


【解决方案1】:

尝试: 1- Spreed 操作员。

var data_name = {...Data[i]};

2- 字符串化对象。

var data_name = JSON.stringify(Data[i]);

3-“分配”方法。

var data_name = Object.assign({}, Data[i]);

【讨论】:

  • 但我想要完整的对象而不仅仅是名称
  • 我的意思是 data_name 只是一个变量应该包含整个对象
  • 好的,我已经编辑了评论,试试上面提到的每一种方法。
  • 还是一样的反应
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-12
  • 2021-12-03
  • 2016-06-16
  • 1970-01-01
  • 2022-01-02
  • 2014-05-04
  • 1970-01-01
相关资源
最近更新 更多