1

I am trying to append some items from an Array into a string but struggling somewhat. I have an array via the following

this.$root.data[1].arrayOfObjects;

This array looks something like this

(2) [{…}, {…}, __ob__: Observer]
    0:
        label: "Value1"
        name: "Value 1"
        value: 1
    1:
        label: "Value2"
        name: "Value 2"
        value: 2

The array could be empty, or it could contain multiple objects. What I am trying to do is merge the values from the name keys from each object in the array into a string, seperated by a comma. So for the above, I expect to be returned

"Value 1, Value 2"

So I was thinking about first assigning the array

const array = this.$root.data[1].arrayOfObjects;

I was then thinking about looping it

for ( var i = 0; i < array.length; i++ ) {

}

This however is where I get lost. I know I could potentially use some ES6 features like map or filter, but not sure how to apply to this?

Any advice appreciated

Thanks

0

1 Answer 1

1

You could map the wwamted property name and join the array to a string.

Methods:

Code:

result = this.$root.data[1].arrayOfObjects
    .map(({ name }) => name)
    .join(', ');
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.