0

I'm using Node.js to make a Axios request to the Vimeo API.

I'm kind of a Noob with Json but I'd like to understand. I made a call to Vimeo with a filter of name, so I get an "array of objects" (I hope that name is correct ) it looks like this below:

 [{ name: 'A Personalized Goal for Anthony' },

 { name: 'Flow access & overview' },

 { name: 'Welcome - 2018 Conference' },

 { name: 'Dropout Video' } ] }

This is simply the names of the videos, however I need to add two keys value pairs with the same value as the name value for a dropdown menu, so it looks like this below:

[{ name: 'A Personalized Goal for Anthony',
 "text": "A Personalized Goal for Anthony",
 "value": "A Personalized Goal for Anthony"  },

 { name: 'Flow access & overview',
   "text": "Flow access & overview"
   "value": "Flow access & overview" },

 { name: 'Welcome - 2018 Conference',
   "text": "Welcome - 2018 Conference"
   "value": "Welcome - 2018 Conference" },

 { name: 'Dropout Video',
   "text": "Welcome - 2018 Conference"
   "value": "Welcome - 2018 Conference" }] 

My app needs the keys "Text" and "Value" for a dropdown menu in order to process the response from the user.

I'm not sure why { name: 'Dropout Video'}, isn't formatted like this {"name":"Dropout Video"},

Is there a way in Node.js to add or duplicate an array using map or push using the same name value while also doing a foreach to this array which reflects the name but with two additional key value pairs with the same Value as the key Name?

This is a dynamic Object as well so when the request is called again it could have many more than just 4 video names returned.

My goal is to add numerous Videos to a Vimeo Album that I want to make the request to, then my app ( Slack chatbot ) then returns a list of options via a drop down menu for the users consideration so that I'm able to see which Name or title they selected and thus want more information on.

Thank you so much any help here. I wish Vimeo had a Text and Value field so I could just filter that instead but then it wouldn't be fun :)

1
  • my logs now just show [{"name":"STEM HI Shorter version","link":"vimeo.com/28736453"}] however I can't target the link value? for whatever reason myJSON.link, or myJSON[0].link doesn't work and am scratching my head on why. when I do console.log(myJSON.link) i get [Function: link] Commented Mar 21, 2019 at 23:09

3 Answers 3

1

You could map through your result, so, for example, after you get the result:

    const movies =  [{ name: 'A Personalized Goal for Anthony' },
    { name: 'Flow access & overview' },
    { name: 'Welcome - 2018 Conference' },
    { name: 'Dropout Video' }];

You could map it to add the other two properties to each one of your objects, the advantage is that you are not mutating your results.

    const mappedMovies = movies.map(movie => {
      return {
        name: movie.name,
        text: movie.name,
        value: movie.name
      }
    );

In that way, you will have the list of objects with the format that you want, now if you need it in json format you could do:

    const JSONString = JSON.stringify(mappedMovies);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you soooo much dude, I've tried others peoples replies but yours is the only one that I understood enough to gain the result I was looking for!! I'm sure the others were good as well. Thank you all! so much
0

Objects are references, so you could just loop through your objects and add the keys.

const videos = [
  { name: 'A Personalized Goal for Anthony' },
  { name: 'Flow access & overview' },
  { name: 'Welcome - 2018 Conference' },
  { name: 'Dropout Video' }
]

for (video of videos) {
    video.text = video.name
    video.value = video.name 
}

console.log(videos)

1 Comment

Thank you for the Help Francios, however I was getting a typescript error on the logs, it did help a lot, so thank you as well.
0

Welcome to SO Kevin,

It looks like you almost provided the answer yourself already...

To get the resulting array you can map through the object and add keys as required:

const slackVideos = videos.map(function (item, label) {
    return {
        ...item, //this ensures all original keys are available, using the ES6 destructuring
        text: item.name,
        value: item.value,
    }
});

Assuming videos is the object received from vimeo, slackVideos will now contain your aditional text & value keys; both containing the name key from the original vimeo object.

4 Comments

Jonas, Thank you! your reply seemed close as well, and if I understood it enough i probably could have gotten it to work. I also had to add movies.data.map(movies => i had to add .data because it was returning .map is not a functions, which i didn't understand.
You're welcome; regarding I'm not sure why { name: 'Dropout Video'}, isn't formatted like this {"name":"Dropout Video"}, that the former is how most javascript parsers will display a js object, the latter is the json format (based on a js object, but not exactly the same). You likely needed the .data due to the way vimeo data is being received
nvm, i was stupid momentarily. thanks. i delete some json things
No problem. A small reminder that common courtesy on SO/Stack Exchange is upvoting answers (and comments) that have helped you, and marking the solution as the accepted answer 😉

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.