从数组键访问对象值

时间:2021-09-01 21:16:32

this might be a stupid question but I'm stuck :(

这可能是一个愚蠢的问题,但我被卡住了:(

I have an object and an array, one is data like this

我有一个对象和一个数组,一个是这样的数据

var pwData = {
  "2_6": {
    "name":"ggregreg",
    "location":"Manchester",
  },
  "2_70": {
    "name":"rwerwer",
    "location":"Solihull"
  },
  "2_59": {
    "name":"Amy",
    "location":"yjhtgeg"
  }

and the other is a reference to an order, like this

另一个是对订单的引用,就像这样

var pwOrder = ["2_70", "2_59", "2_6"];

I'm then trying to console log some data in order of the pwOrder array, using data from pwData. Such as this.

然后我尝试使用pwData中的数据按照pwOrder数组的顺序控制日志记录一些数据。比如这个。

 $.each(pwOrder, function(k)
{
    console.log(pwData[k].name);
}

Which in my mind should log peoples names from pwData in the order defined by pwOrder - however it doesn't work :( Any help much appreciated, pulling my hair out!!

在我的脑海中应该按照pwOrder定义的顺序从pwData记录人名 - 但是它不起作用:(任何帮助都非常感激,拉我的头发!

2 个解决方案

#1


3  

k in your jquery each callback will be the index, not the key itself.

在你的jquery中,每个回调都是索引,而不是密钥本身。

var pwData = {
  "2_6": {
    "name": "ggregreg",
    "location": "Manchester",
  },
  "2_70": {
    "name": "rwerwer",
    "location": "Solihull"
  },
  "2_59": {
    "name": "Amy",
    "location": "yjhtgeg"
  }
};
var pwOrder = ["2_70", "2_59", "2_6"];

$.each(pwOrder, function(index, key) {
  console.log(pwData[key].name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

jquery is not needed for this, however, it can be done using native javascipt

这不需要jquery,但是,它可以使用本机javascipt来完成

var pwData = {
  "2_6": {
    "name": "ggregreg",
    "location": "Manchester",
  },
  "2_70": {
    "name": "rwerwer",
    "location": "Solihull"
  },
  "2_59": {
    "name": "Amy",
    "location": "yjhtgeg"
  }
};
var pwOrder = ["2_70", "2_59", "2_6"];

pwOrder.forEach(function(key) {
  console.log(pwData[key].name);
});

#2


0  

James is correct.

詹姆斯是对的。

$.each(pwOrder, function(k)
{
    console.log(pwData[pwOrder[k]]['name']);
});

Works.

#1


3  

k in your jquery each callback will be the index, not the key itself.

在你的jquery中,每个回调都是索引,而不是密钥本身。

var pwData = {
  "2_6": {
    "name": "ggregreg",
    "location": "Manchester",
  },
  "2_70": {
    "name": "rwerwer",
    "location": "Solihull"
  },
  "2_59": {
    "name": "Amy",
    "location": "yjhtgeg"
  }
};
var pwOrder = ["2_70", "2_59", "2_6"];

$.each(pwOrder, function(index, key) {
  console.log(pwData[key].name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

jquery is not needed for this, however, it can be done using native javascipt

这不需要jquery,但是,它可以使用本机javascipt来完成

var pwData = {
  "2_6": {
    "name": "ggregreg",
    "location": "Manchester",
  },
  "2_70": {
    "name": "rwerwer",
    "location": "Solihull"
  },
  "2_59": {
    "name": "Amy",
    "location": "yjhtgeg"
  }
};
var pwOrder = ["2_70", "2_59", "2_6"];

pwOrder.forEach(function(key) {
  console.log(pwData[key].name);
});

#2


0  

James is correct.

詹姆斯是对的。

$.each(pwOrder, function(k)
{
    console.log(pwData[pwOrder[k]]['name']);
});

Works.