Convert object's properties and values to array of key value pairs

前端 未结 6 2057
长情又很酷
长情又很酷 2020-12-03 01:40

I\'m fairly new to JavaScript and am not sure this is possible to do but basically I would like to take an object and convert it into an array of strings in the format;

6条回答
  •  [愿得一人]
    2020-12-03 02:10

    var object = {
        private_key: "private-key",
        public_key: "public-key"
    };
    
    var array = [];
    for (var prop in object)
        array.push(prop + "=" + object[prop]);
    return array.join(','); // "private_key=private-key,public_key=public-key"
    

    Notice the order is not guaranteed.

提交回复
热议问题