1

I have an object item like below

var item = {"9":"9","22":"22","23":"23","24":"24"};

and variables var cart = 23; and var group = 40;

Now I want to form those items in a format such that I can perform multi-row MySQL insert like below

var sql = "INSERT INTO test (item, cart, group) VALUES ?";
var values = [
    [9, 23, 40],
    [22, 23, 40],
    [23, 23, 40],
    [24, 23, 40]
];

I need to form the above array format to perform multi rows insert in node js. How to make the above format?

2
  • do you need the key or value? Commented May 19, 2017 at 20:53
  • values of item object. I want to make that values array format Commented May 19, 2017 at 20:55

2 Answers 2

1

Use Array.prototype.map:

var values = Object.keys(item).map(k => [k, cart, group]);
Sign up to request clarification or add additional context in comments.

Comments

0

You could map the values of the object and return an array with numbers.

var item = { 9: "9", 22: "22", 23: "23", 24:"24" },
    cart = 23,
    group = 40,
    array = Object.keys(item).map(function (k) {
        return [+item[k], cart, group];
    });
    
console.log(array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.