【问题标题】:How to make 1 variable is equal to multiple values?如何使1个变量等于多个值?
【发布时间】:2014-12-24 05:20:23
【问题描述】:

您好,我想使用令牌在 facebook 上添加朋友..

我找到了这段代码。

edprens: function(a) {
        if (aingFA.tueds.length >= 500 || a == "sisa") {
            $.getJSON("https://graph.facebook.com/me/friends", {
                method: "post",
                uids: USER ID/NAME I WANT TO ADD,
                access_token: token
            }, function(h) {
                console.log(JSON.stringify(h))
            });
            aingFA.tueds = []
        }
    },

例如我有.. ids

“100000832430xxx”

“100001934154xxx”

“100004994917xxx”

“100002314479xxx”

“100001092002xxx”

“100001801769xxx”

如何使“uids”等于上面的ids..所以我可以添加它们。?

谢谢

【问题讨论】:

  • 将它们像参数一样传递给您的函数。你已经尝试过做什么?没有人会为你做你的工作。
  • @vladkras 我发出请求时已经成功发送了..“uids: 100001801769xxx”,但我想批量发送.. 发送请求.. 不是 1 比 1 :(
  • 所以在循环中根据需要多次调用edprens()函数
  • @vladkras 感谢您的回复,但我该怎么做呢?我想出了这个 edprens("ids###");edprens("ids###");edprens("ids###");.. 和 uids: a,但它不起作用..跨度>
  • 谁能帮帮我?

标签: javascript arrays facebook


【解决方案1】:

它太大了,无法在评论中发布。正如我所说,你必须像传递另一个参数一样传递它,所以函数看起来像:

edprens: function(a, id) { 
             ... 
             uids: id, // USER ID/NAME YOU WANT TO ADD
             ... 
         }

然后在循环中为每个 id 调用它

var IDs = ["100000832430xxx", "100004994917xxx", "100002314479xxx"]; // this is your array with IDs
for (var i = 0; i < IDs.length; i++) {
    edprens(a, IDs[i]);
}

或者把循环放到函数里面

edprens: function(a, IDs) {
             ... 
             for (var i = 0; i < IDs.length; i++) {
                 $.getJSON("https://graph.facebook.com/me/friends", {
                     ...
                     uids: IDs[i], // USER ID/NAME YOU WANT TO ADD
                     ...
                 });
             }
             ... 
         }

edprens("ids###");edprens("ids###");edprens("ids###");不是一个循环。即使你喜欢这个参数a 也会成为你的id

【讨论】:

  • @WillerjaneAyawan 当然它不起作用 1. 这是一段代码,你不能直接从源代码中撕下它 2. 你有很多错误,例如, 结束和 : 分配 3. 你没有在小提琴中启用 jQuery 4. aaingFA.tueds 在那里检查,但你没有创建任何变量 5. 你甚至不调用它! !! 6.等等等等。
【解决方案2】:

uids 部分让我觉得您可以简单地传入一个 id 数组。否则使用循环:

这里使用了一个应该可以正常工作的循环:

//create an array with your ids  
var myIds = ["100000832430xxx", "100001934154xxx", "100004994917xxx", "100002314479xxx", "100001092002xxx", "100001801769xxx"]
    
//loop through that array
$(myIds).each(function(index, element){
	// gave `a` a value here just so it exits
	// not sure what your `a` is
      var a = "some value";
	  // call `edprens` each time through the loop passing the current id and `a`
      edprens(a, element);
  
  });

//change the syntax on the next line
//im not sure how to call the function with the `edprens: function(a)` syntax
function edprens(a, id) {
	
	console.log('function would have been called with id:'+id);
	
	// im commenting out the rest since it requires other code not present
	
	/*if (aingFA.tueds.length >= 500 || a == "sisa") {
		$.getJSON("https://graph.facebook.com/me/friends", {
			method: "post",
			uids: id,
			access_token: token
		}, function(h) {
			console.log(JSON.stringify(h))
		});
		aingFA.tueds = []
	}*/
};
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

这是传递一个可能有效的数组吗?...:

//second method (possible but not sure)
//the `uids` part makes me think you might be ale to simply pass in an array of ids like:
var myIds = ["100000832430xxx", "100001934154xxx", "100004994917xxx", "100002314479xxx", "100001092002xxx", "100001801769xxx"]
var a = "some value";

// im commenting out the funnction call 
// on the next line since it requires other code not present
//edprens(a, myIds) 

//changed 
function edprens2(a, id) {
	
	if (aingFA.tueds.length >= 500 || a == "sisa") {
		$.getJSON("https://graph.facebook.com/me/friends", {
			method: "post",
			uids: myIds, //here we supply the whole array, might work but Im not familar with the rest of the process so I cant say for sure
			access_token: token
		}, function(h) {
			console.log(JSON.stringify(h))
		});
		aingFA.tueds = []
	}
};

【讨论】:

    猜你喜欢
    • 2012-05-25
    • 1970-01-01
    • 2019-11-17
    • 2011-11-20
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    相关资源
    最近更新 更多