简体   繁体   English

javascript 对象数组通过特定键值获取 uniq 值

[英]javascript array of objects get uniq values by specific key value

I have bar like:我有这样的酒吧:

在此处输入图像描述

Server returns:服务器返回:

const rows = [
 {type: "global", count: 6, name: "Name", orderliness: 1},
 {type: "global", count: 5, name: "Name", orderliness: 2},
 {type: "local", count: 1, name: "Name", orderliness: 3},
 {type: "global", count: 8, name: "Name", orderliness: 3},
 {type: "local", count: 2, name: "Name", orderliness: 1},
]

And I should get not repeated orderliness and get output:而且我不应该重复orderliness并得到output:

[
 {type: "global", count: 5, name: "Name", orderliness: 2}
]

And push no repeated item to rows like:并且不将重复的项目push送到如下rows

const rows = [
 {type: "global", count: 6, name: "Name", orderliness: 1},
 {type: "global", count: 5, name: "Name", orderliness: 2},
 {type: "local", count: 1, name: "Name", orderliness: 3},
 {type: "global", count: 8, name: "Name", orderliness: 3},
 {type: "local", count: 2, name: "Name", orderliness: 1},
 // this item I pushed
 {type: "local", count: 0, name: "Name", orderliness: 2},
]

I tried do like:我试过这样做:


function getUnique(arr, comp) {
    return (
        arr
            .map((e) => e[comp])
            // store the keys of the unique objects
            .map((e, i, final) => final.indexOf(e) === i && i)
            // eliminate the dead keys & store unique objects
            .filter((e) => arr[e])
            .map((e) => arr[e])
    );
}
getUnique(rows, 'orderliness');

But it returns like:但它返回如下:

[
 {type: "global", count: 6, name: "Name", orderliness: 1},
 {type: "global", count: 5, name: "Name", orderliness: 2},
 {type: "local", count: 1, name: "Name", orderliness: 3},
]

So you have an array with rows where entries are of one of two types ( "global" and "local" ), and you're trying to find rows that only have one type and not the other then, having found them, push new rows for for the "other" type into rows with count set to 0.所以你有一个数组,其中的条目是两种类型之一( "global""local" ),并且你试图找到只有一种类型而不是另一种类型的行,然后找到它们,推送新的将“其他”类型的rows放入count设置为 0 的行中。

Here's how I'd do it (see comments).这是我的做法(见评论)。

For getUnique :对于getUnique

function getUnique(arr, comp) {
    // Get a count of matching objects
    const counts = new Map();
    for (const {[comp]: value} of arr) {
        const count = counts.get(value) || 0; // `get` returns `undefined` if it's
                                              // not there; we replace that with 0
        counts.set(value, count + 1);
    }
    // Return only the unique ones
    return arr.filter(({[comp]: value}) => counts.get(value) === 1);
}

That makes two passes through the array rather than four, taking advantage of Map's binary tree or similar sublinear lookup time.这使得两次通过数组而不是四次,利用 Map 的二叉树或类似的亚线性查找时间。

Then to add the rows:然后添加行:

// Get the unique rows, create new rows for the "other"
// type with count = 0 and push them into `rows`
rows.push(...getUnique(rows, 'orderliness').map(entry => ({
    ...entry,
    type: entry.type === "global" ? "local" : "global",
    count: 0
})));

Live Example:现场示例:

 function getUnique(arr, comp) { // Get a count of matching objects const counts = new Map(); for (const {[comp]: value} of arr) { const count = counts.get(value) || 0; // `get` returns `undefined` if it's // not there; we replace that with 0 counts.set(value, count + 1); } // Return only the unique ones return arr.filter(({[comp]: value}) => counts.get(value) === 1); } const rows = [ {type: "global", count: 6, name: "Name", orderliness: 1}, {type: "global", count: 5, name: "Name", orderliness: 2}, {type: "local", count: 1, name: "Name", orderliness: 3}, {type: "global", count: 8, name: "Name", orderliness: 3}, {type: "local", count: 2, name: "Name", orderliness: 1}, ]; // Get the unique rows, create new rows for the "other" // type with count = 0 and push them into `rows` rows.push(...getUnique(rows, 'orderliness').map(entry => ({...entry, type: entry.type === "global"? "local": "global", count: 0 }))); console.log(rows);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM