JS对象的索引返回对象中的所有值,而不是实际索引 [英] The index of a JS object returns all values in object rather than the actual index

查看:339
本文介绍了JS对象的索引返回对象中的所有值,而不是实际索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我非常迅速地解释这一点,当我从数据库中获取数据时,有一个父对象,并且内部有一个对象数组,我要实现的目标是编写正确的路径,以便我可以访问特定项在数据库中.

Let me explain this really quickly, when I fetch data from the database, there's a parent object and inside there is an array of objects, what I'm looking to achieve is to write the correct path so I can access specific items in the db.

接收数据时,我将存储两个变量,如下所示:

When receiving the data I store two variables like so:

for (let a in obj) {
    list.push(obj[a]);
    keys.push(a);
}
this.setState({
    list: list,
    keys: keys,
});

执行此代码将打印以下内容:

Executing this code will print the following:

console.log(this.state.keys[index]);

HZlgUZ21vKbungnxaxDJXrTUr2z1

HZlgUZ21vKbungnxaxDJXrTUr2z1

这总是返回确切的父对象id

This always returns the exact parent object id

但是问题是我无法访问嵌套项目的索引:

But the issue is that I cannot access the index of the nested items:

console.log(this.state.list[index]);

对象{-LORYsI9mLP8mu_2BTKS:对象,-LORZVOq8SMUgTOPgpXK:对象, -LORZtqZeg3nyOW4p9I1:对象,-LOYbElg81jbPtao2nl4:对象}

Object {-LORYsI9mLP8mu_2BTKS: Object, -LORZVOq8SMUgTOPgpXK: Object, -LORZtqZeg3nyOW4p9I1: Object, -LOYbElg81jbPtao2nl4: Object}

我要查找的是嵌套商品ID,而不是所有值

What I'm looking for is that nested item ID not all the values

我附上了数据库的图片,所以您可以看到它的样子

i attached a picture of the database so you can see how it looks like

以下是视图的最小表示形式:

Here's a minimal representation of the view:

const listItems = this.state.list.map((item, index) =>
        Object.values(item).map(nestedItem => (
<div>
  {nestedItem.title}
  <button
   onClick={() => this.update(index)}
   >
  CLICK
  </button>
</div>
  ))
);

解决起来很容易,因为我仍在学习编程的主要概念,但是这种嵌套对象总是让我感到沮丧.请有人来救我

It's pretty easy to solve it's just that I am still learning main concepts of programming but this nested objects always makes me frustrated. Please someone come to my rescue

推荐答案

您可以尝试在示例中应用以下概念吗?

Can you try to apply the below concept in your example?

考虑您拥有JSON结构,如

Consider you have a JSON structure like

var obj = {
        a: {
             blue:16,
             green:30,
             red:39
           },
        b: {
             orange:56,
             yellow:34,
             white:35    
           },
        c: {
             black:3,
             brown:43,
             purple:12  
            }
        };

现在,当您对此对象运行for...in循环

Now, when you run a for...in loop over this object

for (let key in obj) {
   list.push(obj[key]); 
   keys.push(a);
}

obj[key]将返回值{blue:16,green:30,red:39},该值最终被推入列表数组

obj[key] will return the value {blue:16,green:30,red:39} which ultimately is pushed into the list array

如果您想要蓝色,绿色和红色,可以执行

If you want the values blue, green and red, you can do something like

Object.keys(obj[key]); //will return an array ['blue', 'green', 'red']

这篇关于JS对象的索引返回对象中的所有值,而不是实际索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆