-4

I need help in getting values of specific property of an array of multiple objects I have an array like this:

[
    {
        "id": 1,
        "name": "skill 1"
    },
    {
        "id": 2,
        "name": "skill 2"
    },
    {
        "id": 3,
        "name": "skill 3"
    }]

and I want to get a string like this :

skill 1 - skill 2 - skill 3
3
  • Please read through How do I ask a good question? Your best bet here is to do your research, search for related topics on SO and elsewhere, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example showing your attempt and say specifically where you're stuck. People will be glad to help. (not my downvote) Commented Nov 25, 2021 at 15:13
  • 2
    Too trivial for a full answer myArray.map((v) => v.name).join(" - ") would do the trick. Commented Nov 25, 2021 at 15:13
  • @DBS - Clunky, hard to read, easy to get wrong, and hard to debug. Outside functional programming with predefined, reusable reducer functions, reduce is just an overcomplicated loop. Use a loop instead. Commented Nov 25, 2021 at 15:16

1 Answer 1

0

First step is to extract name. You can use map.

elements.map(x=>x.name)

Second step is to join it to one string using join

const result = elements.map(x=>x.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.