bitcodr
bitcodr

Reputation: 1454

Convert python to javascript

I have a python code .By this code i can searching in all column table. i want to convert that code to javaScript my python code is :

cmd_str = '&filter='
fields = {'f1': 'v1', 'f2': None, 'f3': 34, 'f4': datetime.now()}
for f, v in fields.items():
if v is not None:
if type(v) is str:
cmd_str += '%s|%s' % (f, v)
elif type(v) is datetime:
cmd_str += '{}[{}]={}'.format(f, type(v).__name__, v.isoformat())
else:
cmd_str += '{}[{}]={}'.format(f, type(v).__name__, v)
cmd_str += ','
cmd_str = cmd_str[:-1]
print(cmd_str)

I convert myself but I have some problems

var cmd_str = '&filter=';
var fields = {
    f1 : 'v1',
    f2 : '',
    f3 : 34
    f4 : datetime.now()
};
var i;
for( i = 0; i<fields.length; i++)
{
    if(fields[i][key] !='')
    {
        if(typeof fields[i][key] === string)
        {
            cmd_str += '%s|%s' % (fields[i][key]);
        }
        else if(typeof fields[i][key] === Date)
        {
            cmd_str += '{}[{}]={}'.format(f, typeof v.__name__, v.isoformat())
        }
        else
        {
            cmd_str += '{}[{}]={}'.format(f, typeof v.__name__, v);
            cmd_str += ',';
            cmd_str = cmd_str[:-1];
        }
    }
} 

Can you fix problems for me? I dont know this line is ok in javaScript:

  cmd_str += '%s|%s' % (fields[i][key]);
  cmd_str += '{}[{}]={}'.format(f, typeof v.__name__, v.isoformat())
  cmd_str += '{}[{}]={}'.format(f, typeof v.__name__, v);
  cmd_str += ',';
  cmd_str = cmd_str[:-1];

Upvotes: 0

Views: 424

Answers (3)

Roli Agrawal
Roli Agrawal

Reputation: 2466

I think few mistakes which i can see is 1) Loop structure is wrong . I didn't get what is key. 2) use instance of to check date. Because date is a object so when you do typeof(fields.f4) it will give you result as object. which is not required now. 3)for comma i am putting comma after every condition and at last taking sub string after removing last comma. I tried code like this.

var cmd_str = '&filter=';
var fields = {
f1 : 'v1',
f2 : '',
f3 : 34,
f4 : new Date()
};
var i;
for(i in fields)
{
  if(fields[i]!='')
{
    if(typeof(fields[i]) === "string")
    {
        cmd_str += i +'|'+ fields[i]+',';
    }
    else if(fields[i] instanceof Date)
    {
        cmd_str += i +'[datetime]='+ fields[i]+',';
    }
    else
    {
        cmd_str += i + typeof (fields[i])+"="+fields[i];
        cmd_str += ',';

    }
  }
} 
cmd_str = cmd_str.substring(0, cmd_str.length - 1); 

Upvotes: 1

bitcodr
bitcodr

Reputation: 1454

I changed to this code :

var cmd_str = '&filter=';
var fields = {
f1 : 'v1',
f2 : '',
f3 : 34
f4 : new Date()
};
var i;
for( i = 0; i<fields.length; i++)
{
  if(fields[i][key] !='')
{
    if(typeof fields[i][key] === string)
    {
        cmd_str += fields[i] +'|'+ fields[i][key];
    }
    else if(typeof fields[i][key] === new Date())
    {
        cmd_str += fields[i] +'[datetime]='+ fields[i][key];
    }
    else
    {
        cmd_str += fields[i] +'[typeof fields[i][key]]='+ fields[i][key];
        cmd_str += ',';
        cmd_str = cmd_str[:-1];
    }
  }
} 

Is it right? I don't know how fix this line cmd_str = cmd_str[:-1]; to remove last Comma ,

Upvotes: 0

VahagnNikoghosian
VahagnNikoghosian

Reputation: 613

there is nothing about JS in your code,

  1. string objects does not have format function
  2. % is only numeric operator and doesn't format your string, 'string' % anything = NaN
  3. Date does not have isoformat function use toISOString()
  4. JS not supporting python array [:index] style

Upvotes: 1

Related Questions