如何使1变量等于多个值?

时间:2014-12-24 05:20:23

标签: javascript arrays facebook

您好我想使用令牌在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”等于上面的id。所以我可以添加它们。?

谢谢

2 个答案:

答案 0 :(得分:0)

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

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(&#34; IDS ###&#34); edprens(&#34; IDS ###&#34); edprens(&#34; IDS ###&#34) ;不是循环。即使你喜欢这个参数,a也会成为你的身份

答案 1 :(得分:0)

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 = []
	}*/
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

//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 = []
	}
};