首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过在javascript中保存数据(将数组中的值推入)来减少相同键值的对象数组?

通过在javascript中保存数据(将数组中的值推入)来减少相同键值的对象数组?
EN

Stack Overflow用户
提问于 2020-09-15 20:01:17
回答 4查看 72关注 0票数 0

我有以下格式的数据,基本上是一个对象数组,我已经尝试了几种方法,我如何才能更有效地做到这一点?

代码语言:javascript
复制
const overdue =  [
        {
            "user.employeeId": "10001440",
            "objectives": [
                "Understand the financial indexes."
            ]
        },
        {
            "user.employeeId": "10000303",
            "objectives": [
                "Document preparation & writing skills"
            ]
        },
        {
            "user.employeeId": "10002168",
            "objectives": [
                "Chia ratio setting for Fuze Tea products L11"
            ]
        },

        {
            "user.employeeId": "10002168",
            "objectives": [
                "Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization"
            ]
        },
        {
            "user.employeeId": "10002168",
            "objectives": [
                "Paramix Line 9 setting parameter standardization"
            ]
        },
    ]

如何使用lodash通过JavaScript将其转换为以下内容??

代码语言:javascript
复制
[
        {
            "user.employeeId": "10001440",
            "objectives": [
                "Understand the financial indexs."
            ]
        },
        {
            "user.employeeId": "10000303",
            "objectives": [
                "Document preparation & writing skills"
            ]
        },
        {
            "user.employeeId": "10002168",
            "objectives": [
                "Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization",
                "Paramix Line 9 setting parameter standardization",
                "Chia ratio setting for Fuze Tea products L11"
            ]
        }
    ]

我已经尝试过Array.map了!和正常的逻辑,我们如何使用lodash reduce或arr.reduce更有效地做到这一点呢?

EN

回答 4

Stack Overflow用户

发布于 2020-09-15 20:10:35

您可以使用Array.reduce来实现所需的结果,将数组用作累加器。

我们枚举过期数组中的每个对象,如果它不存在于输出数组中,我们添加它,否则我们将对象目标添加到输出中的相关元素。

代码语言:javascript
复制
const overdue =  [ { "user.employeeId": "10001440", "objectives": [ "Understand the financial indexes." ] }, { "user.employeeId": "10000303", "objectives": [ "Document preparation & writing skills" ] }, { "user.employeeId": "10002168", "objectives": [ "Chia ratio setting for Fuze Tea products L11" ] }, { "user.employeeId": "10002168", "objectives": [ "Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization" ] }, { "user.employeeId": "10002168", "objectives": [ "Paramix Line 9 setting parameter standardization" ] }, ]; 

let result = overdue.reduce((res, row) => {
    let el = res.find(el => el["user.employeeId"] === row["user.employeeId"]);
    // If we find the object in the output array simply update the objectives
    if (el) {
       el.objectives = [...el.objectives, ...row.objectives];
    } else {
    // If we _don't_ find the object, add to the output array.
      res.push({ ...row});
    }
    return res;
}, [])

console.log("Result:",result);

票数 1
EN

Stack Overflow用户

发布于 2020-09-15 20:11:09

使用array.reduce并将对象传递给累加器。在回调函数中,检查累加器是否有与user.employeeId的值相同的键。如果不是,则创建键并将迭代下的当前对象添加为它的值。如果已经有一个键,那么只需更新objectives数组即可。当检索值为Object.value时,它将给出一个数组

代码语言:javascript
复制
const overdue = [{
    "user.employeeId": "10001440",
    "objectives": [
      "Understand the financial indexes."
    ]
  },
  {
    "user.employeeId": "10000303",
    "objectives": [
      "Document preparation & writing skills"
    ]
  },
  {
    "user.employeeId": "10002168",
    "objectives": [
      "Chia ratio setting for Fuze Tea products L11"
    ]
  },

  {
    "user.employeeId": "10002168",
    "objectives": [
      "Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization"
    ]
  },
  {
    "user.employeeId": "10002168",
    "objectives": [
      "Paramix Line 9 setting parameter standardization"
    ]
  },
];



let newData = overdue.reduce((acc, curr) => {
  if (!acc[curr['user.employeeId']]) {
    acc[curr['user.employeeId']] = curr;
  } else {
    curr.objectives.forEach((item) => {
      acc[curr['user.employeeId']]['objectives'].push(item)
    })
  }
  return acc;
}, {});


console.log(Object.values(newData))

票数 1
EN

Stack Overflow用户

发布于 2020-09-15 20:18:12

我不确定如何在lodash中做到这一点,但下面是我只使用普通Javascript就能做到的方法:

代码语言:javascript
复制
const overdue = [{
    "user.employeeId": "10001440",
    "objectives": [
      "Understand the financial indexes."
    ]
  },
  {
    "user.employeeId": "10000303",
    "objectives": [
      "Document preparation & writing skills"
    ]
  },
  {
    "user.employeeId": "10002168",
    "objectives": [
      "Chia ratio setting for Fuze Tea products L11"
    ]
  },

  {
    "user.employeeId": "10002168",
    "objectives": [
      "Brix parameter differences between Processing and Production of Fuze Tea Lemon-Lemongrass standardization"
    ]
  },
  {
    "user.employeeId": "10002168",
    "objectives": [
      "Paramix Line 9 setting parameter standardization"
    ]
  },
];

const combined = {};

overdue.forEach(o => {
  const id = o["user.employeeId"];
  const obj = o["objectives"][0];
  
  if(!combined[id]) { combined[id] = [obj]; }
  else { combined[id].push(obj); }
  
});

const result = [];

Object.keys(combined).forEach(key => {
  result.push({"user.employeeId" : key, "objectives" : combined[key]});
});

console.log(result);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63901400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档