Category: Multiple solutions blog: blog.csdn.net/qtfying Nuggets: juejin.cn/user/430094… QQ: 2811132560 Email: [email protected]
These two days when browsing the blog, found such a problem, try to solve a solution, now the solution of several methods posted for everyone’s reference and learning
The target array is:
let data = [{
id: '1'.goodsName: 'test'.price: 22
},
{
id: '2'.goodsName: 'test'.price: 22
},
{
id: '2'.goodsName: 'test'.price: 22
},
{
id: '3'.goodsName: 'test'.price: 22
},
{
id: '3'.goodsName: 'test'.price: 22
},
{
id: '3'.goodsName: 'test'.price: 22
},
{
id: '3'.goodsName: 'test'.price: 22
},
{
id: '4'.goodsName: 'test'.price: 22}]Copy the code
The target results are:
let data = [{
id: '1'.goodsName: 'test'.price: 22.count:1
},
{
id: '2'.goodsName: 'test'.price: 22.count:2
},
{
id: '3'.goodsName: 'test'.price: 22.count:4
},
{
id: '4'.goodsName: 'test'.price: 22.count:1}]Copy the code
The same items in the object are merged into one item and counted, adding the key of count to the item
- The first solution is the most primitive, the most primitive way to remove weight
var interimArray = []; // Use to store the removed id
var resultArray = []; // Final desired result
var count = 1;
for (var i in data) {
if (interimArray.indexOf(data[i].id) > - 1) {
console.log(`${data[i].id}Already exists, count plus 1 '.` count for${count}`);
for (var j in resultArray) {
if(resultArray[j].id === data[i].id) { count++; resultArray[j].count = count; }}}else {
count = 1;
console.log(`${data[i].id}Not found, array added${data[i].id}`.` count reset to${count}`); interimArray.push(data[i].id); data[i].count = count; resultArray.push(data[i]); }}console.log(JSON.stringify(resultArray, null.2));
Copy the code
- The second solution is to take the id as the key value, determine whether the key value exists, and then fetch the value into an array
let result = {};
for (let i = 0, len = data.length; i < len; i++) {
let dataArr = data[i];
if (result[dataArr.id]) {
result[dataArr.id].count ++;
} else{ result[dataArr.id] = { ... dataArr,count: 1}; }}let resultArr = [];
for (const key in result) {
resultArr.push(result[key]);
}
console.log(JSON.stringify(Object.values(result), null.2));
Copy the code
- The third is similar to the second but uses Object.entries to turn the second Object into a two-dimensional array
// Reduce parameters prev, cur, index, arr
var twoDimensionalArray = Object.entries(data.reduce((prev, cur) = > {
if(! prev[cur.id]) { prev[cur.id] = { ... cur,count: 1
};
} else {
prev[cur.id].count += 1;
}
return prev;
}, {}));
var result = twoDimensionalArray.map(entry= > entry[1]);
console.log(JSON.stringify(result, null.2));
Copy the code
- The fourth approach is to simplify the first approach with the Reduce approach
let result = data.reduce((obj, item) = > {
let find = obj.find(i= > i.id === item.id);
let_d = { ... item,count: 1
};
find ? find.count++ : obj.push(_d);
returnobj; } []);console.log(JSON.stringify(result, null.2));
Copy the code
Small as it is, it has all the five organs, including array de-duplication, Set, reduce, Object.keys (), Object.values(), object.entries, two-dimensional array, three methods of loop: Map, for, reduce, etc