I have a function whatIsInAName(collection)
that prints to the console (as string) every key and property of every object that exists in collection
:
function whatIsInAName(collection) {
for (obj in collection){
for (var [key, value] of Object.entries(collection[obj])) {
console.log(key + value)
}}
}
console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null });
// Resultado:
VM2094:9 firstRomeo
VM2094:9 lastMontague
VM2094:9 firstMercutio
VM2094:9 lastnull
VM2094:9 firstTybalt
VM2094:9 lastCapulet
However, I would like it to print all the information together for each object, that is:
// Resultado:
firstRomeolastMontague
firstMercutiolastnull
firstTybaltlastCapulet
How can it be done? (Yes you can)
Playing a bit with the loops
for
can be achieved in a simple way. Instead ofconsole.log()
eachclave->valor
of the object, what you can do is save it to a cumulative variable to do theconsole.log
to that variable at the end of the inner loop.This works because the first loop iterates through the objects and the second through the properties of each object.
Another option that you have is to directly use a join on
Object.entries
it (and remove the commas that it generates).