如何将单对象数组内的对象值拼接为带空格的单个字符串
单对象数组属性值拼接方案
需求说明
现有仅包含1个对象的数组如下:
[ { "key1":"john", "key2":"smith", "key3":"mark" } ]
需要将对象的所有属性值用空格分隔拼接为单个字符串,最终得到结果:
john smith mark
不同语言实现示例
JavaScript
// 原始数据 const originArr = [ { "key1":"john", "key2":"smith", "key3":"mark" } ] // 处理逻辑:先取数组第一个元素,提取所有属性值后用空格拼接 const res = Object.values(originArr[0]).join(' ') console.log(res) // 输出 john smith mark
Python
# 原始数据 origin_arr = [ { "key1":"john", "key2":"smith", "key3":"mark" } ] # 处理逻辑:先取数组第一个元素,提取所有值后用空格拼接 res = ' '.join(origin_arr[0].values()) print(res) # 输出 john smith mark
PHP
// 原始数据 $originArr = [ [ 'key1' => 'john', 'key2' => 'smith', 'key3' => 'mark' ] ]; // 处理逻辑 $res = implode(' ', array_values($originArr[0])); echo $res; // 输出 john smith mark
内容的提问来源于stack exchange,提问作者Adnan Farogh
相关产品推荐
相关产品推荐

