【问题标题】:Java script create new array grouped by object properties [closed]Javascript创建按对象属性分组的新数组[关闭]
【发布时间】:2021-12-24 12:34:08
【问题描述】:

我正在学习 Java 脚本并尝试根据对象的特定属性合并一个对象数组。

例如,我有以下数组,其中包含属性 a、b、c、pet 和 age 的对象。 如果 2 个对象的属性 a、b、c 相同,我想创建一个包含宠物和年龄分组的新数组。 如果 a、b、c 中的任何属性不匹配,我想将它们作为新对象添加到我的输出数组中。

myArray = [
  {
    a: 'animal',
    b: 'white',
    c: true,  
    pet: 'dog1',
    age: 1  
  },
  {
    a: 'animal',
    b: 'white',
    c: true,
    pet: 'dog2',
    age: 2
  },
  {
    a: 'animal2',
    b: 'white',
    c: true,
    pet: 'cat1',
    age: 5
  },
  {
    a: 'animal2',
    b: 'black',
    c: false,
    pet: 'cat2',
    age: 1
  }
]

按属性 a、b、c 分组的输出数组。我的输出数组的第一个元素包含输入数组中对象 0,1 的组合值,因为它们具有相同的 a、b、c 属性。其余的被添加为单独的值,因为它们在一个属性中不同。

outputArray = [
    {
        a: 'animal',
        b: 'white',
        c: true,
        pets: [{pet:'dog1,age:1},{pet:dog2,age:2}]
    },
    {
        a: 'animal2',
        b: 'white',
        c: true,
        pets: [{pet: 'cat1', age:5}]
    },
    {
        a: 'animal2',
        b: 'black',
        c: false,
        pets:[{pet: 'cat2', age: 1}]
    }
 ]

最后我想要一个数组,其中所有元素按属性 a、b、c 分组。 有没有一种有效的方法?我尝试使用 for 循环进行暴力破解,但没有成功。

TIA。

【问题讨论】:

  • myArray 不是有效数组...
  • @decpk 编辑了它。谢谢
  • 如果 a: "white" 和 b: true 也是特定于给定宠物的变量,也可以将它们移动到 pets 数组中。我还建议使用更具描述性的键,而不是 abc。这些通用键往往会导致代码不可读且难以调试。

标签: javascript arrays object arraylist


【解决方案1】:

1)您可以使用Mapfor..of轻松实现结果

const myArray = [
  {
    a: "animal",
    b: "white",
    c: true,
    pet: "dog1",
    age: 1,
  },
  {
    a: "animal",
    b: "white",
    c: true,
    pet: "dog2",
    age: 2,
  },
  {
    a: "animal2",
    b: "white",
    c: true,
    pet: "cat1",
    age: 5,
  },
  {
    a: "animal2",
    b: "black",
    c: false,
    pet: "cat2",
    age: 1,
  },
];

const dict = new Map();
for (let { a, b, c, ...rest } of myArray) {
  const key = `${a}|${b}|${c}`;
  !dict.has(key)
    ? dict.set(key, { a, b, c, pets: [{ ...rest }] })
    : dict.get(key).pets.push(rest);
}

const result = [...dict.values()];
console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; 

2) 您也可以使用Object.valuesreduce 获得相同的结果

const myArray = [
  {
    a: "animal",
    b: "white",
    c: true,
    pet: "dog1",
    age: 1,
  },
  {
    a: "animal",
    b: "white",
    c: true,
    pet: "dog2",
    age: 2,
  },
  {
    a: "animal2",
    b: "white",
    c: true,
    pet: "cat1",
    age: 5,
  },
  {
    a: "animal2",
    b: "black",
    c: false,
    pet: "cat2",
    age: 1,
  },
];

const result = Object.values(
  myArray.reduce((dict, { a, b, c, ...rest }) => {
    const key = `${a}|${b}|${c}`;
    !dict[key]
      ? (dict[key] = { a, b, c, pets: [{ ...rest }] })
      : dict[key].pets.push(rest);
    return dict;
  }, {})
);

console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0;

【讨论】:

    猜你喜欢
    • 2017-07-17
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    相关资源
    最近更新 更多