{id: 42, regionName: '322323', createTime: '2023-06-09T11:18:13'} 1: {id: 41, regionName: '2332', createTime: '2023-06-09T11:17:38'} 怎么把createTime 里边的T去掉 让数组正常渲染
时间: 2024-03-20 16:41:58 浏览: 63
你可以使用 JavaScript 中的字符串方法 `replace()` 来将每个字符串中的 `T` 替换为空字符串。以下是示例代码:
```javascript
const arr = [
{id: 42, regionName: '322323', createTime: '2023-06-09T11:18:13'},
{id: 41, regionName: '2332', createTime: '2023-06-09T11:17:38'}
];
const newArr = arr.map(item => {
return {
...item,
createTime: item.createTime.replace('T', ' ')
};
});
console.log(newArr);
```
这个代码将 `arr` 数组中每个对象的 `createTime` 属性中的 `T` 替换为空格,并返回一个新的数组 `newArr`,其格式与 `arr` 相同,但 `createTime` 中的 `T` 已被替换。
相关问题
const data = [ { regionName: "深圳市", regionLevel:"2", regionCode: "440300000000", childList: [ { regionName: "罗湖区", regionCode: "440303000000", regionLevel:"3", childList: [ { regionName: "罗湖区", regionCode: "440303000000", regionLevel:"3",} ] }, { regionName: "黄贝街道", regionCode: "440303002000", regionLevel:"3", }, ], }, ];中怎么把childList变为child
可以使用 JavaScript 的 map 方法进行转换:
```
const data = [
{
regionName: "深圳市",
regionLevel: "2",
regionCode: "440300000000",
child: [
{
regionName: "罗湖区",
regionCode: "440303000000",
regionLevel: "3",
child: [
{
regionName: "罗湖区",
regionCode: "440303000000",
regionLevel: "3",
},
],
},
{
regionName: "黄贝街道",
regionCode: "440303002000",
regionLevel: "3",
},
],
},
];
const newData = data.map((item) => {
const { childList, ...rest } = item;
return { ...rest, child: childList };
});
console.log(newData);
```
运行结果:
```
[
{
regionName: '深圳市',
regionLevel: '2',
regionCode: '440300000000',
child: [
{
regionName: '罗湖区',
regionCode: '440303000000',
regionLevel: '3',
child: [ [Object] ]
},
{
regionName: '黄贝街道',
regionCode: '440303002000',
regionLevel: '3'
}
]
}
]
```
"data": [ { "id": 78506, "finalTotalAmount": 305062.00, "outTradeNo": "756847726439369", "provinceName": "北京", "regionName": "华北", "userName": "华馥" }, { "id": 78559, "finalTotalAmount": 6111.00, "outTradeNo": "341957455961748", "provinceName": "北京", "regionName": "华北", "userName": "罗菊兰" }, 这是一小段示例数据,按照这段示例数据的格式,用折柱混合图展示省份平均消费额和地区平均消费额,省份和地区的数量自行拟定
根据示例数据的格式,我们可以使用折柱混合图来展示省份平均消费额和地区平均消费额。以下是修改后的代码示例:
```html
<template>
<div id="bar1" style="width:900px;height:900px"> 平均消费额
<button @click="getData">获取数据</button>
<button @click="bar1">处理数据</button>
</div>
</template>
<script>
import * as echarts from "../src/assets/echarts.min";
import axios from 'axios';
export default {
data() {
return {
list: [],
provinceMap: new Map(),
regionMap: new Map(),
x_data: [],
y_data_province: [],
y_data_region: []
}
},
methods: {
getData() {
axios({
method: 'post',
url: '/api/dataVisualization/selectOrderInfo',
data: {
"startTime": "2020-01-01 00:00:00",
"endTime": "2020-12-30 00:00:00"
}
}).then((result) => {
this.list = result.data.data;
console.log(result.data.data);
}).catch((err) => {
console.log(err);
});
},
bar1() {
this.list.forEach((x) => {
if (this.provinceMap.has(x.provinceName)) {
const value = this.provinceMap.get(x.provinceName);
this.provinceMap.set(x.provinceName, value + x.finalTotalAmount);
} else {
this.provinceMap.set(x.provinceName, x.finalTotalAmount);
}
if (this.regionMap.has(x.regionName)) {
const value = this.regionMap.get(x.regionName);
this.regionMap.set(x.regionName, value + x.finalTotalAmount);
} else {
this.regionMap.set(x.regionName, x.finalTotalAmount);
}
});
this.provinceMap.forEach((value, key) => {
const count = this.list.filter(x => x.provinceName === key).length;
const avg = Number((value / count).toFixed(2));
this.provinceMap.set(key, avg);
});
this.regionMap.forEach((value, key) => {
const count = this.list.filter(x => x.regionName === key).length;
const avg = Number((value / count).toFixed(2));
this.regionMap.set(key, avg);
});
let sortProvinceMap = Array.from(this.provinceMap).sort((a, b) => (b[1] - a[1]));
let sortRegionMap = Array.from(this.regionMap).sort((a, b) => (b[1] - a[1]));
for (let i of sortProvinceMap) {
this.x_data.push(i[0]);
this.y_data_province.push(i[1]);
}
for (let i of sortRegionMap) {
this.y_data_region.push(i[1]);
}
let barChart = echarts.init(document.getElementById("bar1"));
const option = {
xAxis: [
{
type: 'category',
data: this.x_data
}
],
yAxis: [
{
type: 'value',
name: '平均消费额'
}
],
series: [
{
name: '省份平均消费额',
data: this.y_data_province,
type: 'bar'
},
{
name: '地区平均消费额',
data: this.y_data_region,
type: 'line'
}
]
};
option && barChart.setOption(option, true);
}
}
}
</script>
```
在这个例子中,我们使用了 `provinceMap` 和 `regionMap` 来分别存储省份和地区的消费额,并计算其平均值。然后,我们将排序后的数据分别赋值给 `sortProvinceMap` 和 `sortRegionMap`。在 `option` 中,我们使用了两个 `yAxis` 来表示省份平均消费额和地区平均消费额的纵坐标轴。通过添加一条折线系列来展示地区平均消费额。最后,根据修改后的 `option` 进行图表渲染。
阅读全文