Javascript-动态转换js,包括数组
我有这样一个json:Javascript-动态转换js,包括数组,javascript,arrays,node.js,Javascript,Arrays,Node.js,我有这样一个json: var getBody = req.body; console.log(getBody); {"tags":[{"tag":"Peter"},{"tag":"Maria"}],"term":"Jack","type":"name"} SELECT name, abc from table WHERE (name LIKE ? OR abc LIKE ?) AND (name LIKE ? OR abc LIKE ?) AND (name LIKE ? OR abc
var getBody = req.body;
console.log(getBody);
{"tags":[{"tag":"Peter"},{"tag":"Maria"}],"term":"Jack","type":"name"}
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;
我想动态地将其转换为如下内容:
[ '%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
}, [])
我怎样才能得到想要的结果
预期结果:
[ '%Peter%', '%Peter%', '%Maria%', '%Maria%', '%Jack%', 'name' ]
…我在node.js mysql查询中使用此脚本
编辑:
我需要它的SQL在上述情况下如下所示:
var getBody = req.body;
console.log(getBody);
{"tags":[{"tag":"Peter"},{"tag":"Maria"}],"term":"Jack","type":"name"}
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?)也是动态构建的
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}%`);
}
});
应提供所需的输出。请注意,Array.prototype.flatMap
非常新,您可能需要进行多边形填充
应提供所需的输出。请注意,
Array.prototype.flatMap
非常新,您可能需要多填充。您可以再次减少标签数组并将结果推送到原始累加器:
var data={“tags”:[{“tag”:“Peter”},{“tag”:“Maria”}],“term”:“Jack”,“type”:“name”};
var result=Object.entries(data).reduce((a[k,v])=>{
如果(k=='tags'){
a、 推送(…v.reduce((acc,{tag})=>{
acc.push(`%${tag}%`,`%${tag}%`);
返回acc;
}, []));
}否则{
a、 推送(k=='type'?v:`%${v}%`);
}
返回a;
}, []);
控制台日志(结果)
您可以再次减少标签数组,并将结果推送到原始累加器:
var data={“tags”:[{“tag”:“Peter”},{“tag”:“Maria”}],“term”:“Jack”,“type”:“name”};
var result=Object.entries(data).reduce((a[k,v])=>{
如果(k=='tags'){
a、 推送(…v.reduce((acc,{tag})=>{
acc.push(`%${tag}%`,`%${tag}%`);
返回acc;
}, []));
}否则{
a、 推送(k=='type'?v:`%${v}%`);
}
返回a;
}, []);
控制台日志(结果)代码>对于属性“标记”,您的代码是:
if( key === 'tags' ) {
if ( value.length > 1 ) {
for ( let i=0; i < value.length; i++ ){
result.push('%' + value[i] + '%')
}
} else {
result.push('%' + value + '%')
}
对于属性“tags”,您的代码是:
if( key === 'tags' ) {
if ( value.length > 1 ) {
for ( let i=0; i < value.length; i++ ){
result.push('%' + value[i] + '%')
}
} else {
result.push('%' + value + '%')
}
您的代码可以简单地更正,当您遍历数组时,您会得到一个带有key标记的对象,因此您需要按value[i][“tag”]
来获得实际值
var input = JSON.parse('{"tags":[{"tag":"Peter"},{"tag":"Maria"}],"term":"Jack","type":"name"}');//req;
console.log(input);
var sqlVal = Object
.entries(input)
.reduce(function(result, entry) {
console.log(result,entry);
const key = entry[0]
const value = entry[1]
console.log(key,value);
if( key === 'term' ) {
result.push('%' + value + '%')
} else if( key === 'tags' ) {
if ( value.length > 1 ) {
for ( let i=0; i < value.length; i++ ){
console.log("value is",value);
result.push('%' + value[i]["tag"] + '%');//here you are getting object, you should draw the tag value of that object here
}
} 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
}, [])
var input=JSON.parse(“{”标记“:[{”标记“:“彼得”},{”标记“:“玛丽亚”}],“术语“:“杰克”,“类型“:“名称”}”)//请求;
控制台日志(输入);
var sqlVal=Object
.条目(输入)
.reduce(函数(结果、条目){
console.log(结果、条目);
常量键=条目[0]
常量值=条目[1]
console.log(键、值);
如果(键=='term'){
结果.推送('%'+值+'%'))
}else if(键==='tags'){
如果(value.length>1){
for(设i=0;i
您的代码可以简单地更正,当您迭代数组时,您会得到一个带有键标记的对象,因此您需要按value[i][“tag”]
以获得实际值
var input = JSON.parse('{"tags":[{"tag":"Peter"},{"tag":"Maria"}],"term":"Jack","type":"name"}');//req;
console.log(input);
var sqlVal = Object
.entries(input)
.reduce(function(result, entry) {
console.log(result,entry);
const key = entry[0]
const value = entry[1]
console.log(key,value);
if( key === 'term' ) {
result.push('%' + value + '%')
} else if( key === 'tags' ) {
if ( value.length > 1 ) {
for ( let i=0; i < value.length; i++ ){
console.log("value is",value);
result.push('%' + value[i]["tag"] + '%');//here you are getting object, you should draw the tag value of that object here
}
} 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
}, [])
var input=JSON.parse(“{”标记“:[{”标记“:“彼得”},{”标记“:“玛丽亚”}],“术语“:“杰克”,“类型“:“名称”}”)//请求;
控制台日志(输入);
var sqlVal=Object
.条目(输入)
.reduce(函数(结果、条目){
console.log(结果、条目);
常量键=条目[0]
常量值=条目[1]
console.log(键、值);
如果(键=='term'){
结果.推送('%'+值+'%'))
}else if(键==='tags'){
如果(value.length>1){
for(设i=0;i
为什么彼得和玛丽亚重复两次?。。。因为:-)。。。这就是sql的设置方式。因此,在这种情况下,我需要它两次。在示例数据getBody
中,我没有看到这些重复项。json是否始终具有这种格式?。。。对没有。。。但是我需要在期望的结果中复制它为什么彼得和玛丽亚重复两次?。。。因为:-)。。。这就是sql的设置方式。因此,在这种情况下,我需要它两次。在示例数据getBody
中,我没有看到这些重复项。json是否始终具有这种格式?。。。对没有。。。但我需要复制它,以达到预期的效果