X
X
XtReLL2019-11-13 12:20:09
JavaScript
XtReLL, 2019-11-13 12:20:09

How to process an array of attachments VK API objects?

Hello. I get the proposed posts using the wall.get method, for further data processing.
This data is then passed to the wall.post method.
Here is an example of a wall.get request:

const content = await vk.api.wall.get({
     owner_id: config.idGroup,
     filter: 'suggests'
   });
  },

And here is the post request itself, wall.post :
const suggestsPost = await vk.api.wall.post({
      owner_id: config.idGroup,
      post_id: content.id,
      from_group: from_groupFlag,
      signed: signedFlag
    });

The bottom line is, the user offers a post, we get this post and publish it by post id.
Of course, the documentation obliges us to transfer text or attached content (attachments), but if there are no problems with the text, even if it is not sent in the request, then with attachments everything is so good. Of the entire array, only the first element is published. Offered 2 photos, only the first will be published. Due to this, there was a need to pass the attachments parameter in the wall.post method, but it is not clear how to iterate over the properties of the objects themselves and pass them to the wall.post method.
Here is an example of what an array of attachments objects looks like, which we get.
Button
5dcbc2d8390e9456285111.png

And here is an example of how the attachments parameter should look like.
Button
5dcbc38929d74987282814.png

Here's how I'm trying to implement data parsing.
let attachments = content.attachments;//помещаю массив в отдельную переменную
    let attach = new Array();//объявление итогового массива
    for (var i = 0; i < attachments.length; i++) {
      let attachType = attachments[i].type;//тип контента
(x)  let attachId = attachments[i].attachType.id;//id контента
(x)  let attachOwner = attachments[i].attachType.owner_id;//id владельца контента
      attach[i] = attachType + attachOwner + '_' + attachId;
    }
        if (attachments.length > 1) {
      let attachResult = attach.join(',');
    }
    else {
      let attachResult = attach;
    }
    console.log(attachResult);

    const suggestsPost = await vk.api.wall.post({
      owner_id: config.id.groupID2,
      post_id: content.id,
      attachments: attachResult,
      from_group: from_groupFlag,
      signed: signedFlag
    });

Problematic lines are marked with '(x)' . I can't figure out how to refer to an object if its properties depend on each other.
Of course, as an option, you can create a bunch of cases through switch and look at type, but this is not reliable, since any change in the name or adding a new type will lead to a problem. I would like the program to recognize the name itself and be able to refer to it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stockholm Syndrome, 2019-11-13
@XtReLL

read about access through square brackets

const attachType = 'photo'; 

obj.attachType // вернёт obj.attachType
obj[attachType] // вернёт obj.photo

and this sheet is not needed when everything is decided by a couple of lines
const attachments = content.attachments
  .map((o) => `${o.type}${o[o.type].owner_id}_${o[o.type].id}`)
  .join(',');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question