for
(
let
i
=
0
;
i
<
arr
.
length
;
i
++
)
{
let
index
=
idArr
.
indexOf
(
arr
[
i
]
.
id
)
if
(
index
>
-
1
)
{
resultArr
[
index
]
.
value
+=
arr
[
i
]
.
value
}
else
{
idArr
.
push
(
arr
[
i
]
.
id
)
resultArr
.
push
(
arr
[
i
]
)
如果项目中处理这种数据多的话,我们可以
封装成一个方法
,
传入数组
即可。
import { delSomeObjValue } from '@/utils/tool.ts'
const originArr = [
{ id: '1', value: 10 },
{ id: '1', value: 20 },
{ id: '2', value: 10 },
{ id: '88', value: 2 },
{ id: '88', value: 5 },
{ id: '20', value: 50 }
console.log(delSomeObjValue(originArr, 'id', 'value'))
arr 需要处理的数组
keyName 用于判断相同的键名
keyValue 用于计算的键值
export function delSomeObjValue(arr:any[], keyName:string, valueName:string) {
const idArr:any[] = []
const resultArr:any[] = []
for (let i = 0; i < arr.length; i++) {
const index = idArr.indexOf(arr[i][keyName])
if (index > -1) {
resultArr[index][valueName] += arr[i][valueName]
} else {
idArr.push(arr[i][keyName])
console.log(idArr)
resultArr.push(arr[i])
return resultArr
方案2:reduce+find实现,这个方案也是更推崇的。
function sameReduce(arr: any[]) {
let result = arr.reduce((acc, cur) => {
let found = acc.find((item: any) => {
return item.id === cur.id
if (found) {
found.value += cur.value
} else {
acc.push({ id: cur.id, value: cur.value })
return acc
}, [])
return result
console.log(sameReduce(originData1), 'Id相同的value相加')
这是网上的例子,觉得思路很不错,拿来练习的。
假设需要处理的数组结构。
const originData = [
{ name: 'tony', id: '1', age: '20' },
{ name: 'jack', id: '2', age: '21' },
{ name: 'tony', id: '3', age: '50' },
{ name: 'jack', id: '4', age: '10' },
{ name: 'mark', id: '5', age: '22' },
{ name: 'mark', id: '6', age: '40' }
方案1:合并实现代码
onMounted(() => {
const originData = [
{ name: 'tony', id: '1', age: '20' },
{ name: 'jack', id: '2', age: '21' },
{ name: 'tony', id: '3', age: '50' },
{ name: 'jack', id: '4', age: '10' },
{ name: 'mark', id: '5', age: '22' },
{ name: 'mark', id: '6', age: '40' }
const nameArr = []
const resultData = []
for (let i = 0; i < originData.length; i++) {
if (nameArr.indexOf(originData[i].name) === -1) {
resultData.push({
name: originData[i].name,
origin: [originData[i]]
nameArr.push(originData[i].name)
} else {
for (let j = 0; j < resultData.length; j++) {
if (resultData[j].name === originData[i].name) {
resultData[j].origin.push(originData[i])
break
console.log(resultData, 'console.log(resultData)')
打印结果:
方案2:针对方案1进行优化
function sameArr(arr: any[]) {
let result = Object.values(
arr.reduce((acc, cur) => {
if (acc[cur.name]) {
acc[cur.name].origin.push(cur)
} else {
acc[cur.name] = { name: cur.name, origin: [cur] }
return acc
}, {})
return result
console.log(sameArr(originData2), 'sameArr')
之前也写过类似合并重组数组的文章,vue把多种类型的数据进行合并,相同类型的数据相加
async mergeOrder(orderList) {
console.log(orderList);
let contrast_1 = JSON.parse(JSON.stringify(orderList));
for(let x in contrast_1){
contrast_1[x].trade_price = this.toD
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X...
六国破灭,非兵不利 ,战不善,弊在赂秦。赂秦而力亏,破灭之道也。或曰:六国互丧,率赂秦耶?曰:不赂者以赂者丧,盖失强援,不能独完。故曰:弊在赂秦也。 秦以攻取之外,小则获邑,大则得城。较秦之所得,与战胜而得者,其实百倍;诸侯之所亡,与战败而亡者,其实亦百
07-28
let arr=[{id:1,num:5},{id:2,num:2},{id:1,num:5}];
function getNewlist(oldArr){
var temp={};
for(var i in oldArr){
var id=oldArr[i].id;
if(temp[id]){
temp[id].num+=oldArr[i].num;
}else{
temp[id]=oldArr[i]
var newArr=[];
for(var j in