Javascript-动态转换包括数组的js

时间:2018-11-29 19:05:32

标签: javascript arrays node.js

我有一个像这样的json:

var getBody = req.body;

console.log(getBody);

{"tags":[{"tag":"Peter"},{"tag":"Maria"}],"term":"Jack","type":"name"}

我想将其动态转换为……:

[ '%Peter%', '%Peter%', '%Maria%', '%Maria%', '%Jack%', 'name' ]        

...所以如果需要,我需要两次每个标记值。

当前我有这个脚本:

var input = req;
var sqlVal = Object
    .entries(input.body)
    .reduce(function(result, entry) {
        const key = entry[0]
        const value = entry[1]
    if( key === 'term' ) {
    result.push('%' + value + '%')
    } else if( key === 'tags' ) {
        if ( value.length > 1 ) {
            for ( let i=0; i < value.length; i++ ){
                result.push('%' + value[i] + '%')
            }
        } else {
            result.push('%' + value + '%')
        }
    } else if( key === 'type' ) {
        if ( value.includes(",")  ) {
            var array = value.split(",");
            for ( let i=0; i < array.length; i++ ){
                result.push(array[i])
            }
        } else {
            result.push(value)
        }
    }
    return result
    }, [])

...但是我得到了:

console.log(sqlVal);

[ '%[object Object]%', '%[object Object]%', '%Jack%', 'name' ]  

如何获得所需的结果?

所需结果:

[ '%Peter%', '%Peter%', '%Maria%', '%Maria%', '%Jack%', 'name' ]

...我在node.js mysql查询中使用此脚本

编辑:

我需要的SQL在上述情况下看起来像这样:

SELECT name, abc from table WHERE (name LIKE ? OR abc LIKE ?) AND (name LIKE ? OR abc LIKE ?) AND (name LIKE ? OR abc LIKE ?) ORDER BY name;    

注意:(名称LIKE?或abc LIKE?)也是动态生成的。

4 个答案:

答案 0 :(得分:4)

ggplot() +
  geom_point(data = Short_cortex5, aes(x = Short_cortex5$SEPT2, y = Short_cortex5$MARC1)) +
  geom_smooth(data = Short_cortex5, aes(x = Short_cortex5$SEPT2, y = Short_cortex5$MARC1), method = lm, se = FALSE) +
  geom_point(data = Short_cortex5, aes(x = Short_cortex5$SEPT2, y = Short_cortex5$MARC2)) +
  geom_smooth(data = Short_cortex5, aes(x = Short_cortex5$SEPT2, y = Short_cortex5$MARC2), method = lm, se = FALSE)

应提供所需的输出。请注意,Object.values(req.body) .flatMap(x => { switch (Object.prototype.toString.call(x)) { case '[object String]': return `%${x}%`; case '[object Array]': return x.map(y => `%${y.tag}%`); } }); 很新,您可能需要填充。

答案 1 :(得分:1)

您可以再次reduce标签数组,并将结果推送到原始累加器:

var data = {"tags":[{"tag":"Peter"},{"tag":"Maria"}],"term":"Jack","type":"name"};

var result = Object.entries(data).reduce((a, [k, v]) => {
  if (k === 'tags') {
    a.push(...v.reduce((acc, {tag}) => {
                  acc.push(`%${tag}%`, `%${tag}%`);
                  return acc;
                }, []));
  } else {
    a.push(k === 'type' ? v : `%${v}%`);
  }
  return a;
}, []);

console.log(result);

答案 2 :(得分:1)

您的代码可以简单地更正,当您遍历数组时,您会得到一个带有键标签的对象,因此您需要按#4 0x00005654135cad52 cc_shell_model_set_panel_visibility (gnome-control-center)才能获得实际值。

value[i]["tag"]

答案 3 :(得分:0)

对于属性“标签”,您的代码为:

if( key === 'tags' ) {
    if ( value.length > 1 ) {
        for ( let i=0; i < value.length; i++ ){
            result.push('%' + value[i] + '%')
        }
    } else {
        result.push('%' + value + '%')
    }

但是在这种情况下,value是一个对象数组(在示例中),而不是字符串。因此value[i]类似于{ tag: "Peter" }。当您将其与%串联时,该对象被强制为字符串,即“ [object Object]”。取而代之的是,您将要在标记对象中检索值

            result.push('%' + value[i].tag + '%')
   //                                 ^^^^