这样的东西也许能帮到你
@KafkaListener(topics = { "order-updated","order-created","order-rejected","order-shipped","order-cancelled","order-delivered","order-packed","order-accepted","update","created","reject","shipped","cancel" },groupId = "group_id",containerFactory = "kafkaListenerContainerFactoryMain")
public void consumeOmsEvents(@Header(KafkaHeaders.RECEIVED_TOPIC) String topic,@Payload(required = false) Message message) {
System.out.println("Got NEW Event for topic from kafka: " + topic);
System.out.println("Received: " + topic + " to: " + message.getTargetUrl());
try {
subscriberService.sendWebhook(message);
} catch (Exception e) {
if (message.getRetryCount() < retryCount) {
kafkaTemplate.send("event-retry",message);
}
}
System.out.println("Success: " + message.getTargetUrl());
}
@KafkaListener(topics = { "event-retry" },containerFactory = "kafkaListenerContainerFactoryRetry")
public void consumeRetryOmsEvents(@Header(KafkaHeaders.RECEIVED_TOPIC) String topic,@Payload(required = false) Message message) {
System.out.println("xxxxxxxxxxxxxxxxxx Got Retry Event for topic from kafka: xxxxxxxxxxxxxxxxxx" + topic);
System.out.println("Received Retry: " + topic + " to: " + message.getTargetUrl() + " count ="+message.getRetryCount());
try {
if (message.getRetryCount() < retryCount) {
subscriberService.sendWebhook(message);
} else {
subscriberService.disableSubscriber(message);
}
} catch (Exception e) {
if (message.getRetryCount() < retryCount) {
message.setRetryCount(message.getRetryCount() + 1);
kafkaTemplate.send("event-retry",message);
}
}
}
,
阅读对您问题的编辑,也许这更有用:
使用 Map
,对于每个 key
,您都有一个包含值的数组。
const response = [
{id:"haskjdhak",name: "lorem ipsun 1",desc: "any description 1"},{id:"78khasc",name: "lorem ipsun 2",desc: "any description 2"},{id:"56jkjn",name: "lorem ipsun 3",desc: "any description 3"}
]
const map = new Map();
response.map(m => (Object.keys(m).forEach(e => {
map.get(e) === undefined ? map.set(e,[m[e]]) : map.get(e).push(m[e])
} )))
map.forEach((value,key) => {
console.log(`${key} = ${value}`);
})
同样使用 node.JS
直接使用 console.log(map)
可以看到:
Map(3) {
'id' => [ 'haskjdhak','78khasc','56jkjn' ],'name' => [ 'lorem ipsun 1','lorem ipsun 2','lorem ipsun 3' ],'desc' => [ 'any description 1','any description 2','any description 3' ]
}
这段代码从您的对象中获取所有键并使用该键创建一个 Map
。如果该键不存在任何值,则添加一个具有初始值的数组;如果存在一个值(不是 undefined
),则它 push()
新值。
此外,使用这种方法,您的数据可以包含不同的键,例如,使用此条目数据:
const response = [
{ id: "haskjdhak",desc: "any description 1" },{ id: "78khasc",desc: "any description 2" },{ id: "56jkjn",desc: "any description 3" },{
id: "1"
},{
new: "value"
},{
id: "new",name: "new",desc: "new",other: "new"
}
]
结果是:
Map(5) {
'id' => [ 'haskjdhak','56jkjn','1','new' ],'lorem ipsun 3','desc' => [
'any description 1','any description 3','new'
],'new' => [ 'value' ],'other' => [ 'new' ]
}
-- 原答案--
如果我理解正确,你可以试试这个:
const response = [
{id:"haskjdhak",desc: "any description 3"}
]
var result = response.map(m => ({key:(Object.keys(m).join(",")),value:(Object.values(m).join(","))}))
console.log("result = ",result)
这段代码创建了一个包含 key/value
对象的数组,其中 key
是键(一个包含所有值的字符串),而 value
是相同的值。
本文链接:https://www.f2er.com/10147.html