Answer the question
In order to leave comments, you need to log in
How to merge a JS object?
Good day.
I'm trying to get a complete list of my friends with this query:
https://api.vk.com/method/friends.get?count=${$count}&offset=${$offset}&fields=${$fields}&access_token=${$token}&v=5.103
offset
. ajax
nothing came to my mind other than using it. let alles1 = $.ajax({
url: `https://api.vk.com/method/friends.get?count=${$count}&offset=${$offset}&fields=${$fields}&access_token=${$token}&v=5.103`,
type: 'GET',
dataType: 'jsonp',
crossDomain: true,
success: async (data) => {
return await data
}
})
let alles2 = $.ajax({
url: `https://api.vk.com/method/friends.get?count=${$count}&offset=${$offset2}&fields=${$fields}&access_token=${$token}&v=5.103`,
type: 'GET',
dataType: 'jsonp',
crossDomain: true,
success: async (data) => {
return await data
}
})
Object.assign()
, yes, this method combines, but bitch REPLACES the keys of the object. Answer the question
In order to leave comments, you need to log in
Thanks to everyone for the answers, and especially to Sergey Sokolov .
Decision:
let alles1 = await $.ajax({
url: `https://api.vk.com/method/friends.get?count=${$count}&offset=${$offset}&fields=${$fields}&access_token=${$token}&v=5.103`,
type: 'GET',
dataType: 'jsonp',
crossDomain: true,
success: async (data) => {
return await data
}
})
let alles2 = await $.ajax({
url: `https://api.vk.com/method/friends.get?count=${$count}&offset=${$offset2}&fields=${$fields}&access_token=${$token}&v=5.103`,
type: 'GET',
dataType: 'jsonp',
crossDomain: true,
success: async (data) => {
return await data
}
})
const alles = alles1.response.items.concat(alles2.response.items)
console.log(alles)
use https://vk.com/dev/execute
in code write something like this:
var members = API.friends.get({
"count": 5000,
"fields": "photo_max",
"offset": 0,
"v": 5.103
}).items;
members = members + API.friends.get({
"count": 5000,
"fields": "photo_max",
"offset": 5000,
"v": 5.103
}).items;
return members;
It’s better to get everything in one request through the VK method execute()
- it allows you to execute simple code, in a JS-like language, on the VK server side:
const code = `
var offset = 0;
var step = 5000;
var result = API.friends.get({"offset":offset, "count":step});
var friends = result.items;
var total = result.count;
while(friends.length < total) {
offset = offset + step;
friends = friends + API.friends.get({
"offset": offset,
"count": step,
}).items;
}
return friends;
`;
new Promise((res, rej) => {
$.ajax({
url: 'https://api.vk.com/method/execute',
method: 'POST',
data: {
code: code,
v: '5.103',
access_token: 'SECRET',
},
success: data => res(data),
error: err => rej(err),
});
}).then(data => {
console.log('Все друзья:', data);
})
.catch(err => console.error(err));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question