Java腳本創建按對象屬性分組的新數組

我正在學習Java腳本,并嘗試根據對象的特定屬性合并對象數組。

例如,我有以下數組,其中包含屬性a、b、c、pet和age的對象。如果兩個對象的屬性a、b、c相同,我想創建一個新陣列,將pet和年齡分組。如果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.

? 最佳回答:

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}|$|${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實現samer結果

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}|$|${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;

主站蜘蛛池模板: 久久国产精品免费一区| 亚洲欧美日韩中文字幕一区二区三区| 中文字幕一区二区三区在线播放| 久久亚洲中文字幕精品一区四 | 精品乱人伦一区二区| 精品无码一区二区三区爱欲九九| 风流老熟女一区二区三区| 亚洲色偷精品一区二区三区| 中文字幕无码免费久久9一区9| 日韩在线一区视频| 无码成人一区二区| 中文字幕一区二区三区有限公司 | 亚洲毛片αv无线播放一区| 国产精品综合一区二区三区| 78成人精品电影在线播放日韩精品电影一区亚洲 | 精品无码人妻一区二区免费蜜桃 | 国模极品一区二区三区| 无码人妻一区二区三区免费手机 | 精品一区二区三区在线视频观看| 亚洲无码一区二区三区| 亚洲AV无码一区二区乱子仑| 在线观看国产一区二三区| 麻豆aⅴ精品无码一区二区| 久久精品一区二区东京热| 日本一区二区三区在线观看| 无码一区二区三区视频| 亚洲综合一区二区国产精品| 久久久久99人妻一区二区三区 | 激情啪啪精品一区二区| 免费一区二区无码视频在线播放| 国模一区二区三区| 日本免费精品一区二区三区| 国产AV国片精品一区二区| 国产一区二区精品尤物| 免费无码毛片一区二区APP| 亚洲av日韩综合一区二区三区| 国产香蕉一区二区三区在线视频| 国内精品一区二区三区在线观看| 美女福利视频一区二区| 国产一区二区三区在线2021| 色老头在线一区二区三区|