0

I am using following line of code to split an array of objects to comma separated values

var myArray = [{Name: "apple"}, {Name: "Banana "}];
type : { DisplayText: myArray.length > 0 ? myArray.map(function (elm) { return elm.Name }).join(", ") : null,
         property1: 'a',
         Property2: 'b'
       }

Output:

apple, Banana 

However, how i can generate the following output

apple
Banana

Note: The following does not work. It generates output apple Banana

DisplayText: myArray.length > 0 ? myArray.map(function (elm) { return elm.Name }).join("\n") : null,

Update: Actually i am creating type object with DisplayText property and two other properties. That's why i have , at the end of line . Have a look at my updated answer

10
  • 3
    .join("\n") does that Commented Jul 3, 2017 at 6:22
  • No, that generates out apple Banana Commented Jul 3, 2017 at 6:23
  • yes i updated my question, my array is exactly in the format you said, it was just type while posting this question.I repeat my array is array of objects with Name property in each object Commented Jul 3, 2017 at 6:25
  • @JapanGuy, As i said before "\n" does not work at all. Dont understand why people upvoted your answer even though i clearly said It does not work Commented Jul 3, 2017 at 6:30
  • 1
    still works with your new code jsfiddle.net/3y8jqged Commented Jul 3, 2017 at 6:42

3 Answers 3

2

Its working remove the , with end of the displaytext null; instead of null,

var myArray = [{Name: "apple"}, {Name: "Banana "}];
var type = { DisplayText: myArray.length > 0 ? myArray.map(function (elm) { return elm.Name }).join("\n ") : null,
         property1: 'a',
         Property2: 'b'
       }
  console.log(type.DisplayText)

Using html try with join('<br>')

var myArray = [{Name: "apple"}, {Name: "Banana "}];
var type = { DisplayText: myArray.length > 0 ? myArray.map(function (elm) { return elm.Name }).join("<br> ") : null,
         property1: 'a',
         Property2: 'b'
       }
document.body.innerHTML +=type.DisplayText

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

1 Comment

please have a look at my updated question. There is a reason i was putting , at the end of line
0

Try This

   var myArray = ["apple", "Banana "];
DisplayText: myArray.length > 0 ? myArray.map(function (elm) { return elm.Name }).join("\n") : null,

Comments

0

Use array#join method with , as separator

var myArray = ["apple", "Banana "];
console.log(myArray.join(','));

It will take , as default separator if no symbol is passed

var myArray = ["apple", "Banana "];
    console.log(myArray.join());

Edit(after question update)

var arr = [{
  Name: "apple"
}, {
  Name: "Banana "
}];
var newStr = '';

var x = arr.map(function(item) {
  newStr = item.Name;
  return newStr;

}).join();

console.log(x)

2 Comments

Please check my updated question. My array is an array of objects and not array of strings. e.g. var myArray = [{Name: "apple"}, {Name: "Banana "}];
.join(',') and .join() are functionally identical since the default join character is ",".

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.