i need to add bracket on every two objects so this input from db foreach
var Categories = [
{ "text": "Mobile Recharge", "callback_data": 1 },
{ "text": "Internet", "callback_data": 2 },
{ "text": "Digital Code", "callback_data": 3 },
{ "text": "Tv", "callback_data": 4 }
];
and this is output i need , if there is 5 objects the last will be only inside bracket and so on.
var Categories = [
[ { "text": "Mobile Recharge", "callback_data": 1 },{ "text": "Internet", "callback_data": 2 } ],
[ { "text": "Digital Code", "callback_data": 3 },{ "text": "Tv", "callback_data": 4 } ],
];
this is full code
let Categories = [];
bot.hears('🛒 Categories', ctx => {
db.query(`SELECT * FROM cat WHERE status = 1`, function(error, res) {
res.forEach(function(row) {
var cat = { text: row.title , callback_data: row.id }
Categories.push(cat);
});
var InlineKeyboardMarkup = {
'inline_keyboard': [Categories]
}
ctx.reply("Select a Category : \r", {
"reply_markup": JSON.stringify(InlineKeyboardMarkup)
});
Categories = []
})
})
This should do it: a simple for( ... ) loop
var Categories = [
{ "text": "Mobile Recharge", "callback_data": 1 },
{ "text": "Internet", "callback_data": 2 },
{ "text": "Digital Code", "callback_data": 3 },
{ "text": "Tv", "callback_data": 4 },
{ "text": "something else", "callback_data": 5 }
];
const res=[];
for (let i=0;i<Categories.length; i+=2) res.push(Categories.slice(i,i+2))
console.log(res)