1

I've an array which has objects inside it:

[
{pointID: 1, pointName: "One" },
{pointID: 2, pointName: "Two" },
{pointID: 3, pointName: "Three" }
]

I would like to join all the strings i.e pointName like this : "One-Two-Three"

Any help is appreciated. Thanks.

Edit 1:

This is what I tried so Far:

this.viaPoints.forEach(x=> {      
      var splitted = x.pointName.split(" ");    
      console.log('I5: ',splitted[0].join("-"));
     });

The reason I've split is because the string will have extra characters some times like "One - A". So I've used split to remove the extra

Edit 2: In my case this helped as I mentioned above that to remove extra characters and strings:

var permittedValues1 = this.viaPoints.map(value => value.pointName.split(" ")[0]).join("-") ;
1
  • 1
    What have you tried so far ?? Please always mention what you have already attempted !! "I've a programming problem, solve it for me !!" is not how SO is supposed to be used. Commented Sep 16, 2021 at 8:19

2 Answers 2

2

You can use .map and .join functionalities too. Here .map will take the values based upon the key-pointName and .join is used to return an array as a string. The elements will be separated by a specified separator (here "-").

let array = [
      {pointID: 1, pointName: "One" },
      {pointID: 2, pointName: "Two" },
      {pointID: 3, pointName: "Three" }
      ];
var permittedValues = array.map(value => value.pointName).join("-") ;
Sign up to request clarification or add additional context in comments.

5 Comments

If i want to apply join for pointID and pointName at sametime, how should we do.?
public permittedValues = this.array.map(value => value.pointID.toString().concat(value.pointName)).join("-") ;
Thank you for your response. I meant to say, I have a table. In the first column I want to display all pointID's with a comma, and in the second column all pointNames,.sameway for the third column...
Why don't you use the ngFor directive for the above array? like this <table style="border:1px solid green;"> <tr *ngFor="let x of array"> <td style="border:1px solid green;">{{x.pointID}},</td> <td style="border:1px solid green;">{{x.pointName}}</td> <td style="border:1px solid green;">{{x.pointID}} -{{x.pointName}}</td> </tr> </table>
I figured it out, This is my solution for the query i have. const clusterNames= this.getpolicies.map(item=>item.cluster_name).join(', '); const policyNames = this.getpolicies.map(item => item.templateName).join(', '); const policyLabel = this.getpolicies.map(item => item.policy_name).join(', '); const policyyml = this.getpolicies.map(item => item.templateYaml); const tableEntry = { clusterNames, policyNames, policyLabel, policyyml } this.tableDetails = [...this.tableDetails,tableEntry]
2

You can use Array.prototype.reduce function to return an array which contains pointName for each object in the array and join all value of the return array with -.

let data = [
  {pointID: 1, pointName: "One" },
  {pointID: 2, pointName: "Two" },
  {pointID: 3, pointName: "Three" }
]

let result = data.reduce((accumulator, current) => {
    return accumulator.concat(current.pointName);
}, []).join('-');

console.log(result);

1 Comment

.reduce(... => accumulator.concat(...)) is precisely what .map does.

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.