admin管理员组

文章数量:1416082

Here I want to convert object properties to ma separated values like following- Join the elements of an array into a string:

var fruits = {"f1":"Banana", "f2":"Orange", "f3":"Apple","f4":"Mango"};
var energy = Object.keys(fruits).map(function(k){return fruits[k]}).join(",");

The result of energy will be:

Banana,Orange,Apple,Mango

But When I apply like this-

 var fruits = {"f1":"Banana", "f2":"null", "f3":"Apple","f4":"Mango"};
 var energy = Object.keys(fruits).map(function(k){return fruits[k]}).join(",");

The result of energy is like this:

Banana,,Apple,Mango

And I want result like this-

Banana,null,Apple,Mango

I have tried these links but No luck found.

Easy way to turn properties of Javascript object into ma-separated list?

Updated the Question

var fruits = {"f1":"Banana", "f2":"", "f3":"Apple","f4":"Mango"};
var energy = Object.keys(fruits).map(function(k){return fruits[k]}).join(",");

The result of energy is like this:

Banana,,Apple,Mango

I want result like this-

 Banana,null,Apple,Mango

Here I want to convert object properties to ma separated values like following- Join the elements of an array into a string:

var fruits = {"f1":"Banana", "f2":"Orange", "f3":"Apple","f4":"Mango"};
var energy = Object.keys(fruits).map(function(k){return fruits[k]}).join(",");

The result of energy will be:

Banana,Orange,Apple,Mango

But When I apply like this-

 var fruits = {"f1":"Banana", "f2":"null", "f3":"Apple","f4":"Mango"};
 var energy = Object.keys(fruits).map(function(k){return fruits[k]}).join(",");

The result of energy is like this:

Banana,,Apple,Mango

And I want result like this-

Banana,null,Apple,Mango

I have tried these links but No luck found.

Easy way to turn properties of Javascript object into ma-separated list?

Updated the Question

var fruits = {"f1":"Banana", "f2":"", "f3":"Apple","f4":"Mango"};
var energy = Object.keys(fruits).map(function(k){return fruits[k]}).join(",");

The result of energy is like this:

Banana,,Apple,Mango

I want result like this-

 Banana,null,Apple,Mango
Share Improve this question edited Nov 10, 2017 at 6:46 sapics 1,1248 silver badges22 bronze badges asked Nov 10, 2017 at 6:20 Vikas GuptaVikas Gupta 1,2311 gold badge12 silver badges27 bronze badges 8
  • 3 Is it "null" or null ? ( and you may use Object.values(fruits) ...) – Jonas Wilms Commented Nov 10, 2017 at 6:22
  • 1 It works for me. – castletheperson Commented Nov 10, 2017 at 6:24
  • If it’s actually nullreturn String(fruits[k]). – Ry- Commented Nov 10, 2017 at 6:24
  • 1 Possible duplicate of How to convert key-value pair object into an array of values in ES6? – Shubham Khatri Commented Nov 10, 2017 at 6:26
  • 1 Sorry, I have updated the question. Please Check Jonas Ryan Killer Death – Vikas Gupta Commented Nov 10, 2017 at 6:31
 |  Show 3 more ments

3 Answers 3

Reset to default 6

Using Object.values would be easier.

// for "null"
var fruits = {"f1":"Banana", "f2":"null", "f3":"Apple","f4":"Mango"};
var energy = Object.values(fruits).join(",");
console.log(energy);

// for null
var fruits = {"f1":"Banana", "f2":null, "f3":"Apple","f4":"Mango"};
var energy = Object.values(fruits).map(String).join(",");
console.log(energy);

// for ""
var fruits = {"f1":"Banana", "f2":"", "f3":"Apple","f4":"Mango"};
var energy = Object.values(fruits).map(v=>v===""?"null":String(v)).join(",");
console.log(energy);

var fruits = {"f1":"Banana", "f2":"", "f3":"Apple","f4":"Mango"};
var energy = Object.keys(fruits).map(function(k){
    if(fruits [k] ==="") return "null";
    return fruits [k]        
    }).join(",");

about join() ,not show value null as string "null", you can check it in map() same as below :

const fruits = {"f1":"Banana", "f2":"", "f3":"Apple","f4":"Mango"};
const energy = Object.values(fruits).map(function(value){
  return !value ? "null" : value;
}).join(",");
console.log(energy);

本文标签: converting object properties to comma seperated values in JavascriptStack Overflow