admin管理员组

文章数量:1279242

I have some JSON data as follows:

{ 
    version: 1,
    partitions: { 
        '0': [ 1616133379 ], 
        '1': [ 1616133378 ], 
        '2': [ 1616133380 ] 
    } 
}

I am looping through the data using async.each as follows:

async.each(topicData.partitions, function(data, callback){
    console.log('/brokers/topics/' + topic + '/partitions/' + data + '/state');
    callback();
},
function(err){
    if(err) {
        console.log(err);
        callback(err);
    }
});

The output I'm getting is:

'/brokers/topics/testing/partitions/1616133379/state' '/brokers/topics/testing/partitions/1616133378/state' '/brokers/topics/testing/partitions/1616133380/state'

As you can see the data item passed through the async.each function is holding the value of the key/value pair whereas I actually want it to pass the key to produce this output:

'/brokers/topics/testing/partitions/0/state' '/brokers/topics/testing/partitions/1/state' '/brokers/topics/testing/partitions/2/state'

Is there anyway I can get the key passed as opposed to the value?

This has to be run asynchronously.

Thanks

I have some JSON data as follows:

{ 
    version: 1,
    partitions: { 
        '0': [ 1616133379 ], 
        '1': [ 1616133378 ], 
        '2': [ 1616133380 ] 
    } 
}

I am looping through the data using async.each as follows:

async.each(topicData.partitions, function(data, callback){
    console.log('/brokers/topics/' + topic + '/partitions/' + data + '/state');
    callback();
},
function(err){
    if(err) {
        console.log(err);
        callback(err);
    }
});

The output I'm getting is:

'/brokers/topics/testing/partitions/1616133379/state' '/brokers/topics/testing/partitions/1616133378/state' '/brokers/topics/testing/partitions/1616133380/state'

As you can see the data item passed through the async.each function is holding the value of the key/value pair whereas I actually want it to pass the key to produce this output:

'/brokers/topics/testing/partitions/0/state' '/brokers/topics/testing/partitions/1/state' '/brokers/topics/testing/partitions/2/state'

Is there anyway I can get the key passed as opposed to the value?

This has to be run asynchronously.

Thanks

Share Improve this question edited Jul 2, 2015 at 8:55 TaoPR 6,0523 gold badges27 silver badges38 bronze badges asked Jul 2, 2015 at 8:50 Jon HunterJon Hunter 8802 gold badges8 silver badges18 bronze badges 2
  • possible duplicate of async.js each get index in iterator – Andreas Louv Commented Jul 2, 2015 at 8:57
  • 1 Google async.each key -> [I Feel Lucky] – Andreas Louv Commented Jul 2, 2015 at 8:57
Add a ment  | 

2 Answers 2

Reset to default 9

You could use forEachOf, the iterator gets passed the value and key of each item in case of an object.

iterator(item, key, callback) - A function to apply to each item in obj. The key is the item's key, or index in the case of an array. The iterator is passed a callback(err) which must be called once it has pleted. If no error has occurred, the callback should be run without arguments or with an explicit null argument.

Usage:

async.forEachOf(topicData.partitions, function(item, key, callback){
  console.log('/brokers/topics/' + topic + '/partitions/' + key + '/state');
  callback();
}, function(err){
  if(err) {
    console.log(err);
    callback(err);
  }
});

Try to put this in your loop.

console.log(topicData.partitions.indexOf(data));

本文标签: javascriptGet object key using asynceachStack Overflow