将对象转换为数组
<script>
// 将对象转换为数组
const obj = {
0000: "zs",
0001: "ls",
0002: "ww",
};
const res = Object.entries(obj).map(([key, value]) => ({
value: key.padStart(4, '0'),
label: value,
}));
console.log(res);
/*
结果:
[
{
"value": "0000",
"label": "zs"
},
{
"value": "0001",
"label": "ls"
},
{
"value": "0002",
"label": "ww"
}
]*/
</script>
计算json数据里每项的和,最终返回每项求和后的数组
<script>
// 计算json数据里每项的和,最终返回每项求和后的数组
const dayPlanOutput = [
{
"2023-12-18": 10,
"2023-12-19": 0,
"2023-12-20": 0,
"2023-12-21": 0,
"2023-12-22": 0,
"2023-12-23": 0,
"2023-12-24": 0
},
{
"2023-12-18": 1,
"2023-12-19": 0,
"2023-12-20": 0,
"2023-12-21": 0,
"2023-12-22": 0,
"2023-12-23": 0,
"2023-12-24": 0
},
{
"2023-12-18": 0,
"2023-12-19": 3,
"2023-12-20": 0,
"2023-12-21": 0,
"2023-12-22": 1994,
"2023-12-23": 2659,
"2023-12-24": 0
},
{
"2023-12-18": 0,
"2023-12-19": 0,
"2023-12-20": 0,
"2023-12-21": 0,
"2023-12-22": 0,
"2023-12-23": 0,
"2023-12-24": 0
},
{
"2023-12-18": 0,
"2023-12-19": 0,
"2023-12-20": 0,
"2023-12-21": 0,
"2023-12-22": 0,
"2023-12-23": 0,
"2023-12-24": 10
}
];
const sumsArr = Object.keys(dayPlanOutput[0]).map(date => {
return dayPlanOutput.reduce((acc, obj) => acc + obj[date], 0);
});
console.log(sumsArr); // [11, 3, 0, 0, 1994, 2659, 10]
</script>
从arr数据里过滤出只含有str数据,最终返回json
<script>
// 从arr数据里过滤出只含有str数据,最终返回json
const arr = [
{bfId: "aaa", bfName: "111"},
{bfId: "bbb", bfName: "222"},
{bfId: "ccc", bfName: "333"},
]
const str = "aaa,ccc";
function convertStringToArray(str, dataArray) {
const selectedItems = str.split(',').map(item => item.trim());
return dataArray.filter(item => selectedItems.includes(item.bfId));
}
const resultArray = convertStringToArray(str, arr);
console.log(resultArray);
/*
结果:
[
{"bfId": "aaa", "bfName": "111"},
{"bfId": "ccc", "bfName": "333"}
]*/
</script>
将objNew数据复制并替换给objOld
<script>
// 将objNew数据复制并替换给objOld
let objOld = {
publishFlag: '0',
loading: false
};
let dataNew = {
publishFlag: '1',
a: '111'
};
let res = Object.assign({}, objOld, dataNew);
console.log(res);
/*
结果:
{
"publishFlag": "1",
"loading": false,
"a": "111"
}*/
</script>
文章来源地址https://www.toymoban.com/news/detail-824741.html
文章来源:https://www.toymoban.com/news/detail-824741.html
到了这里,关于JavaScript数据处理案例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!