简体   繁体   English

将对象的属性转换为逗号分隔的列表?

[英]Turn properties of object into a comma-separated list?

I have an object like this:我有一个这样的对象:

var person = {
  name: "John",
  surname: "Smith",
  phone: "253 689 4555"
}

I want:我想:

John,Smith,253 689 4555

Is there some easy way?有什么简单的方法吗?

If possible, could you please provide an example where I can also define the separator?如果可能的话,您能否提供一个我也可以定义分隔符的示例?

您可以在现代浏览器中使用此单线

Object.keys(person).map(function(k){return person[k]}).join(",");

This code will be useful when you want to extract some property values from an array of objects as comma separated string.当您想从对象数组中提取一些属性值作为逗号分隔的字符串时,此代码将很有用。

    var arrayObjects = [
  {
    property1: "A",
    property2: "1"
  },
  {
    property1: "B",
    property2: "2"
  },
  {
    property1: "C",
    property2: "3"
  }
];

Array.prototype.map.call(arrayObjects, function(item) { return item.property1; }).join(",");

Output - "A,B,C"输出- “A,B,C”

Here is a simpler one liner.这是一个更简单的一个班轮。 It uses the Object.values() method instead of Object.keys它使用Object.values()方法而不是 Object.keys

Object.values(obj).join(",");

Write a function like this:写一个这样的函数:

function toCSV(obj, separator) {
    var arr = [];

    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            arr.push(obj[key]);
        }
    }

    return arr.join(separator || ",");
}

And then you can call it like:然后你可以这样称呼它:

toCSV(person, "|"); // returns "John|Smith|253 689 4555"

or或者

toCSV(person); // returns "John,Smith,253 689 4555"

try this:尝试这个:

var key,
  person = {
    name: "John",
    surname: "Smith",
    phone: "253 689 4555"
  },
  array = [];

for ( key in person ) {
  if ( person.hasOwnProperty( key ) ) {
    array.push( person[ key ] );
  }
}

console.log( array.join( ',' ) );

or in function style:或功能风格:

var
  getValues = function ( obj ) {
    var key,
      array = [];

    for ( key in obj ) {
      if ( obj .hasOwnProperty( key ) ) {
        array.push( obj [ key ] );
      }
    }

    return obj.join( ',' );
  };

var person = {
      name: "John",
      surname: "Smith",
      phone: "253 689 4555"
    };

console.log( getValues( person ) );
var arr = [
    {
        key_1: "A",
        key_2: "1"
    },
    {
        key_1: "B",
        key_2: "2"
    },
    {
        key_1: "C",
        key_2: "3"
    }
];

var CSVOf_arr = arr.map((item) => { return item.key_1 }).join(',')

console.log('Comma seprated values of arr', CSVOf_arr)

OUTPUT: A,B,C输出:A、B、C

You might want to try this library - Underscore.js .您可能想试试这个库 - Underscore.js It gives you really helpful methods and works across browsers.它为您提供了非常有用的方法,并且可以跨浏览器工作。

Here you can do it like this -在这里你可以这样做 -

_.values(person).join(",")

or或者

_.values(person).join("|")

The Object.values() method returns an array of a given object's own enumerable property values. Object.values()方法返回给定对象自己的可枚举属性值的数组。 It can be used to convert object properties to an array which can then be joined using .join(separator) to create the required string with separator defined in join function.它可用于将对象属性转换为数组,然后可以使用.join(separator)将其连接起来,以创建所需的字符串,并在连接函数中定义分隔符。 But this will cause empty values to be joined as well.但这也会导致空值被加入。 So for an object as所以对于一个对象

var person = {
  name: "John",
  surname: "Smith",
  phone: ""
}

output for输出为

Object.values(person).join(',')

will be将会

John,Smith,约翰·史密斯,

with an additional "," at the end.最后加上一个“,”。 To avoid this .filter() can be used to remove empty elements from array and then use join on filtered array.为了避免这种情况,可以使用.filter()从数组中删除空元素,然后在过滤后的数组上使用连接。

Object.values(person).filter(Boolean).join(',')

will output将输出

John,Smith约翰·史密斯

Object.values(person).join('/')

Another way is to use lodash function _.toString()另一种方法是使用 lodash 函数 _.toString()

console.log( _.toString( Object.values( person ) ) );
==> John,Smith,253 689 4555

Check the link below https://lodash.com/docs/4.17.5#toString检查下面的链接https://lodash.com/docs/4.17.5#toString

var person = {
    name: "John",
    surname: "Smith",
    phone: "253 689 4555"
 }



let personData = ${person.name},${person.surname},${person.phone}

With this `` mark and $ dollar sign you will get the result.使用这个 `` 标记和 $ 美元符号,您将得到结果。

只需使用 object.values() 并使用 toString() 方法

Object.values(person).toString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM