0

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];
4
  • 2
    Can you tell us what are the problems you're facing? Commented Feb 20, 2016 at 16:41
  • 2
    I think datetime is not valid in JS. Try using new Date()? Commented Feb 20, 2016 at 16:42
  • also typeof returns a string in javascript. Can't compare to Date object Commented Feb 20, 2016 at 16:53
  • 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]; Commented Feb 20, 2016 at 16:56

3 Answers 3

1

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); 
Sign up to request clarification or add additional context in comments.

1 Comment

thanks , but after each field should be a comma , and last field comma most be remove
1

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

Comments

0

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 ,

7 Comments

No still this code is not right few points 1) What is key. 2) type of date is Object . you are doing that wrong use instanceof for date
the key. i mean value
key is not reserved word
how can i fix it for date?
How can I get the value in fields[i]?
|

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.