0

The way I'm doing it is to save that properties into an array and after that to join them.

Original array of objects:

const objArray = [ { prop: "a", etc: 1}, { prop: "b", etc: 2}, { prop: "c", etc: 3} ];

First step, save the values of property prop into an array:

const firstStep = objArray.map(a => a.prop);

Second step, concatenate them into a string:

const secondStep = firstStep.join(' + ');

This works fine but I'm thinking if there is a better/shorter method to do this. Any ideas?

1 Answer 1

1

It isn't much, but you can chain the operations together, there's no need to save the intermediate result in a variable first:

const result = objArray
  .map(a => a.prop)
  .join(' + ');

const objArray = [ { prop: "a", etc: 1}, { prop: "b", etc: 2}, { prop: "c", etc: 3} ];
const result = objArray
  .map(a => a.prop)
  .join(' + ');
console.log(result);

Other than that, I don't think it's possible to implement the logic in a shorter fashion.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.