0

For that code below the output of ins (lets assume the value of keys are summer, spring, fall, winter) is:

['req.body.summer, req.body.spring, req.body.fall, req.body.winter']

What i want is remove the string after the replace command, so i can insert it to my SQLite query. So the output must be:

[req.body.summer, req.body.spring, req.body.fall, req.body.winter]

Im just new into programming so please bear with me. Thank you!

var arr = '';

Object.keys(input).forEach(function(key) {
  arr += 'req.body.' + key + ', ';
});

var ins = [arr.replace(/,\s*$/, '')];

console.log(ins);
7
  • Do you want the output as an array? Commented Apr 28, 2017 at 4:54
  • Yes, No strings @SankarRaj Commented Apr 28, 2017 at 4:55
  • What is input? An object? Commented Apr 28, 2017 at 4:55
  • If input is an array - hence Object.keys will give "key" as indexes i.e. 0 , 1, 2 .. hence the output will be req.body.0,req.body.1 ..etc Commented Apr 28, 2017 at 4:56
  • 1
    This is not clear: the input is an array of strings? Like 'summer'? Commented Apr 28, 2017 at 5:01

3 Answers 3

1

Try this...

Just declare arr as an array and Push the values into it.

var arr = [];
var input = ['summer', 'spring', 'fall', 'winter'];
(input).forEach(function(key,value) {
  arr.push('req.body.' + key);
});


console.log(arr);

Sign up to request clarification or add additional context in comments.

2 Comments

is it possible to remove the string in every element on array?
How will i remove the "" in ["req.body.spring"]? I only want [req.body.spring only]. Because in that, it will print the text req.body.spring, what i need is not string, a variable. req.body.spring will get the input value from the EJS. Im very sorry
1

What you are looking for an Array of String Objects

var arr = new Array() ;var input= { 'spring':'a','summer': 'b'}
    Object.keys(input).forEach(function(key) {
      arr.push('req.body.' + key);
    });
    console.log(arr); // Prints ["req.body.spring", "req.body.summer"]

2 Comments

How will i remove the "" in "req.body.spring"? I only want req.body.spring only.
@JohnCarloVelasquez - You cannot remove the double quotes. this is a string literal - How do you want to use the Output of the program
1

If the input is an array of strings, like 'summer', then do this:

var arr = input.map(key => req.body[key]);

// Sample input
var input = ['summer', 'spring', 'fall', 'winter'];

// Sample req:
var req = {
    body: {
        summer: 'sun',
        spring: 'birds',
        fall: 'leaves',
        winter: 'snow'
    }
};

var arr = input.map(key => req.body[key]);

console.log(arr);

If you prefer the function syntax instead of =>:

var arr = input.map(function (key) {
    return req.body[key];
});

5 Comments

It is JavaScript. Did you try to run the snippet? Maybe you don't know the arrow function syntax?
Oh sorry, didnt know that, is it possible to remove the stringify of it? i only want [ sun, birds, leaves, snow ]
What is sun if it is not a string? A variable? If it is a variable, then it will still look up the value of that variable, which again could be a string. Not sure what you want. Console output is a string.
How will i remove the "" in ["req.body.spring"]? I only want [req.body.spring only]. Because in that, it will print the text req.body.spring, what i need is not string, a variable. req.body.spring will get the input value from the EJS. Im very sorry
Please formulate your question clearly. The quotes you see are just representation. They are not actually stored. If the value you want to get is not yet there then process the input later when the values are available.

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.