Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Javascript join data from datamap?

I know it's much more a Javascript question, but anyway...

Let's say I have this...
(print: $weapons.join(", "))
...and the weapons var is an array with datamaps, so if it has 2 objects, it will be printed as [object Map], [object Map]. How can I print a list of each object by one of its values inside a datamap, if possible?

This is the structure of each datamap inside the array $weapons:
(datamap:
	"Name","Hammer",
	"Description","Yeah, it's a hammer.",
	"slot","right_hand"
)
I want to produce a list of "Name"s...

Comments

  • You can use the Javascript Array map() method to create a temporary Array containing the value of each element's Name property, and then use the Javascript Array join method to concatenate the elements of the temporary array into a String.
    (set: $weapons to (array:
    	(datamap:
    		"Name", "Hammer",
    		"Description", "Yeah, it's a hammer.",
    		"slot", "right_hand"
    	),
    	(datamap:
    		"Name", "Sword",
    		"Description", "Yeah, it's a sword.",
    		"slot", "right_hand"
    	),
    	(datamap:
    		"Name", "Dagger",
    		"Description", "Yeah, it's a dagger.",
    		"slot", "right_hand"
    	)))
    	
    (print: $weapons.map(function(item){ return item.get("Name"); }).join(', '))
    
    warning: The above example does not include any error checking code.
  • Great, thanks a lot, it solves the problem.
  • One more thing: any chance to do the same, but merging duplicated values? So if the array had two with the name "Dagger", the join would result in "Hammer, Dagger 2x"?
Sign In or Register to comment.