Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
add bracket on every two objects

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 = []
    })
})
about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

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)

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!