How can I retrieve the key-value pairs of Objects that are in an Array?
Using JavaScript, I am trying to store the key value of objects to use them in my layout however I please. I know the map method might be my best bet but I am having a hard time understanding how it works.
...and you want to convert it to a desired structure; you are right in assuming the use of a map operation, as one of the ways.
In JavaScript, arrays have a built-in map method, and in your case, you might do something like below:
var desiredOutput = posts.map(
function (post) {
return`<li>${post.name}</li>`;
}
);
As you see above, map takes a function as an argument, which is run on every item of the array; and an array of all the results from this function, is what you would find in desiredOutput.
If I am not wrong, it seems that you are using Polymer; if you are, I believe that 'Template repeaters' would be the way to go, in your case: polymer-project.org/1.0/docs/devguide/templates
Sai Kishore Komanduri
Engineering an eGovernance Product | Hashnode Alumnus | I love pixel art
Hey @adriane, before I attempt an answer, could you share an example on how you have your data structured, and an example of your desired output.
UPDATE:
So if you've an array like this:
const posts = [ { "id": 1, "name": "Something", }, { "id": 2, "name": "Another", }, ]...and you want to convert it to a desired structure; you are right in assuming the use of a
mapoperation, as one of the ways.In JavaScript, arrays have a built-in
mapmethod, and in your case, you might do something like below:var desiredOutput = posts.map( function (post) { return `<li>${post.name}</li>`; } );As you see above,
maptakes a function as an argument, which is run on every item of the array; and an array of all the results from this function, is what you would find indesiredOutput.If I am not wrong, it seems that you are using Polymer; if you are, I believe that 'Template repeaters' would be the way to go, in your case: polymer-project.org/1.0/docs/devguide/templates