我有Set
个userId
字符串,如下所示:
["1", "2", "3"....]
我想遍历并将其转换为这种对象格式:
[
{
"field" : "tag",
"key" : "user_id",
"relation": "=",
"value" : 1 // Insert userId here
},
etc...
]
然后在userId
对象之间,我需要添加对象:
{ "operator": "OR" }
所以数组看起来像
[{userId object}, {OR object}, {userId object}, etc...]
userIds
有任意数量-可能有数千个。我需要做的是,如果数组中有200个以上的对象,则需要使用该对象数组调用函数sendObjects(array)
,然后重置该数组并从中断的地方继续。数组不能以OR object
结尾。怎么做??
答案 0 :(得分:0)
在数组forEach()
上使用numArray
:
var numArray = ["1", "2", "3"];
var res = [];
numArray.forEach(function(val, index){
var obj = {
"field" : "tag",
"key" : "user_id",
"relation": "=",
"value" : val
};
res.push(obj);
if(numArray.length-1 !== index){
res.push({ "operator": "OR" });
}
});
console.log(res);