数组对象中相同的参数拼接成一个对象
Posted 晚星@
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数组对象中相同的参数拼接成一个对象相关的知识,希望对你有一定的参考价值。
//原始数组
let _arr = [
key: '1', name: '1', datas: [111] ,
key: '1', name: '1', datas: [112] ,
key: '1', name: '2', datas: [121] ,
key: '1', name: '3', datas: [131] ,
key: '2', name: '1', datas: [211] ,
key: '2', name: '1', datas: [212] ,
key: '2', name: '2', datas: [221] ,
key: '3', name: '1', datas: [311] ,
key: '3', name: '2', datas: [321] ,
key: '3', name: '2', datas: [322] ,
key: '3', name: '2', datas: [323] ,
key: '3', name: '2', datas: [324] ,
key: '3', name: '3', datas: [331] ,
]
let tempKeyArr = [];
var flag = false
var flagCount = 0
let tempKeyName = [];
let tempDataArr = [];
let detailDatas = []
for (let i = 0; i < _arr.length; i++)
flag = false
flagCount = 0
if (i == 0)
tempDataArr.push(
key: _arr[i].key,
name: _arr[i].name,
datas: _arr[i].datas
);
tempKeyArr.push(_arr[i].key);
tempKeyName.push(_arr[i].name);
else
for (let k = 0; k < tempKeyArr.length; k++)
if (tempKeyArr[k] != _arr[i].key || tempKeyName[k] != _arr[i].name)
flagCount++
if (flagCount == tempKeyArr.length)
tempDataArr.push(
key: _arr[i].key,
name: _arr[i].name,
datas: _arr[i].datas
);
tempKeyArr.push(_arr[i].key);
tempKeyName.push(_arr[i].name);
else
for (let j = 0; j < tempDataArr.length; j++)
if (tempDataArr[j].key == _arr[i].key && tempDataArr[j].name == _arr[i].name)
_arr[i].datas.map(item =>
tempDataArr[j].datas.push(item);
)
detailDatas = tempDataArr
console.log(detailDatas);
以上是关于数组对象中相同的参数拼接成一个对象的主要内容,如果未能解决你的问题,请参考以下文章