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
2 Answers
Reset to default 9You 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
版权声明:本文标题:javascript - Get object key using async.each - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741231783a2362242.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论