想要获取数组密钥的值



我有一个复杂的数组,想找到它的值。但是问题是答案大约是0,1,2,3,4,5

以下是获取数组状态值的代码

var shardState = Object.keys(mydata.cluster.collections[collectionName].shards[String(shardName)].state);
alert(shardState);

以下是数组。

{
  "responseHeader":{
    "status":0,
    "QTime":4},
  "cluster":{
    "collections":{
      "college":{
        "pullReplicas":"0",
        "replicationFactor":"1",
        "shards":{"shard1":{
            "range":"80000000-7fffffff",
            "state":"active",

似乎您正在这样做:

console.log("Your result:", Object.keys("active"))
console.log("'active' converted to object:", Object("active"))

object.keys返回对象的键。由于您通过了一个字符串,因此它返回所有字符的索引。
因此,请从您的代码中删除Object.keys

const mydata = {
  "responseHeader": {
    "status": 0,
    "QTime": 4
  },
  "cluster": {
    "collections": {
      "college": {
        "pullReplicas": "0",
        "replicationFactor": "1",
        "shards": {
          "shard1": {
            "range": "80000000-7fffffff",
            "state": "active",
          }
        }
      }
    }
  }
}
const collectionName = "college", shardName = "shard1";
var shardState = mydata.cluster.collections[collectionName].shards[String(shardName)].state;
console.log(shardState);

var shardstate = mydata.cluster.collections [collectionName] .shards [shardname] .state;警报(shardstate(;

最新更新