js 将[ { "name":"广告测试组", "detail":{ "thrift":200, "web":100, "jar":50 } }, { "name":"广告测试组", "detail":{ "thrift":200, "web":100, "jar":50 } } ] 转换成三个map
时间: 2023-06-28 09:08:29 浏览: 40
可以使用循环遍历数组,然后将每个元素中的 `detail` 属性转换成一个新的 Map 对象,最后将新的 Map 对象添加到数组中。以下是示例代码:
```javascript
const arr = [
{ "name":"广告测试组", "detail":{ "thrift":200, "web":100, "jar":50 } },
{ "name":"广告测试组", "detail":{ "thrift":200, "web":100, "jar":50 } }
];
const maps = [];
for (const item of arr) {
const map = new Map();
for (const key in item.detail) {
map.set(key, item.detail[key]);
}
maps.push(map);
}
console.log(maps);
```
输出结果:
```
[
Map(3) { 'thrift' => 200, 'web' => 100, 'jar' => 50 },
Map(3) { 'thrift' => 200, 'web' => 100, 'jar' => 50 }
]
```
相关问题
js 将[ { "name":"广告测试组", "detail":{ "thrift":200, "web":100, "jar":50 } }, { "name":"广告测试组", "detail":{ "thrift":200, "web":100, "jar":50 } } ] 转换成三个map
你可以使用 JavaScript 的 map 方法来将输入转换为三个 map。以下是一个实现示例:
```javascript
const input = [
{
"name": "广告测试组",
"detail": {
"thrift": 200,
"web": 100,
"jar": 50
}
},
{
"name": "广告测试组",
"detail": {
"thrift": 200,
"web": 100,
"jar": 50
}
}
];
const map1 = input.map(item => ({
name: item.name,
thrift: item.detail.thrift
}));
const map2 = input.map(item => ({
name: item.name,
web: item.detail.web
}));
const map3 = input.map(item => ({
name: item.name,
jar: item.detail.jar
}));
console.log(map1);
console.log(map2);
console.log(map3);
```
输出:
```
[
{ name: '广告测试组', thrift: 200 },
{ name: '广告测试组', thrift: 200 }
]
[
{ name: '广告测试组', web: 100 },
{ name: '广告测试组', web: 100 }
]
[
{ name: '广告测试组', jar: 50 },
{ name: '广告测试组', jar: 50 }
]
```
以上代码将输入数组中的每个对象映射到三个新的对象中,分别包含每个对象的 `name` 属性和 `detail` 对象中的一个属性。
pyhive 执行报错:thrift.Thrift.TApplicationException: Invalid method name: 'GetLog'
这个错误可能是由于客户端和服务器之间的 Thrift 协议不兼容引起的。请确保使用的 pyhive 版本与 Hive 服务器使用的 Thrift 版本兼容。你可以查看 Hive 版本的文档,以确定它使用的 Thrift 版本。
如果你使用的是较新版本的 pyhive,它可能使用的是较新版本的 Thrift 协议。在这种情况下,你可以尝试将 pyhive 的 Thrift 协议版本设置为与 Hive 服务器兼容的版本,例如:
```python
from pyhive import hive
from thrift.protocol import TBinaryProtocol
# 建立连接
host = 'your-hive-host'
port = 10000
conn = hive.Connection(host=host, port=port, username='your-username', password='your-password',
thrift_transport=None, thrift_protocol=TBinaryProtocol)
# 执行查询
cursor = conn.cursor()
cursor.execute('SELECT * FROM your_table')
# 获取结果
results = cursor.fetchall()
# 关闭连接
cursor.close()
conn.close()
```
在上面的示例中,我们将 pyhive 的 Thrift 协议版本设置为 TBinaryProtocol,这是 Hive 服务器支持的协议版本之一。如果你的 Hive 服务器使用的是不同的协议版本,请相应地更改 pyhive 的 Thrift 协议版本。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)