I
I
Ilkhomjon Matazimov2020-04-22 18:39:33
JavaScript
Ilkhomjon Matazimov, 2020-04-22 18:39:33

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


But they have one "but", and it lies in the fact that the maximum you can return is 5k, so I use offset.

So, I need to get 10k friends, and ajaxnothing came to my mind other than using it.

Therefore, I ask you for help.

Below is the code, but you can write your own solution with code, because I think it's not a perfect solution.

The code:

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
    }
})


If, all this shit is output to the console, then we get two response objects.

Then it occurred to me to combine these objects with the method Object.assign(), yes, this method combines, but bitch REPLACES the keys of the object.

How to get all 10k friends if there is another way?
Or how to merge these objects without REPLACING the key?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ilhomjon Matazimov, 2020-04-22
@mr_qpdb

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)

You just had to add await wherever it was needed.

I
iBird Rose, 2020-04-22
@iiiBird

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;

G
grvctr, 2020-04-22
@grvctr

https://developer.mozilla.org/en/docs/Web/JavaScri... Enjoy your
health

S
Sergey Sokolov, 2020-04-22
@sergiks

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 question

Ask a Question

731 491 924 answers to any question