วิธีรวมค่าต่างๆของมพล่ามคำเชยๆออกมาไม่หยุดกุญแจอยู่ในตารางคู่ลำดับของวัตถุที่มีได้หลายที่เพิ่งถูกสร้างกุญแจไว้?

0

คำถาม

มันมีอยู่ก่อนข้อตารางคู่ลำดับของสิ่งเหมือนดังนั้นอยู่ไหนนั่นเป็น'หมวดหมู่'กุญแจและบาง'ชุด'กุญแจ

arrOne = [
    {
        "series_1": 25,
        "category": "Category 1",
        "series_2": 50
    },
    {
        "series_1": 11,
        "category": "Category 2",
        "series_2": 22
    },
    {
        "series_1": 32,
        "category": "Category 1",
        "series_2": 74
    },
    {
        "series_1": 74,
        "category": "Category 3",
        "series_2": 98
    },
    {
        "series_1": 46,
        "category": "Category 3",
        "series_2": 29
    },

]

(โปรดสังเกตว่า'หมวดหมู่'สามารถเป็นอะไรก็ค่าแม้ว่ามันจะเป็นหลายคล้ายกันค่าเช่นเดียวบางพิเศษค่า e.g. มีหลายวัตถุด้วย'หมวดหมู่'ค่า'หมวดหมู่ 3'แต่มีแค่ 1 กับ'หมวดหมู่'ค่า'หมวดหมู่ที่ 2')

ต่อไปนี้บรรทัดของรหัสจะเพิ่มขึ้นมาทั้งหมดขอ series_1 สำหรับวัตถุกับเดียวกันหมวดหมู่

        var objForAllCategories = {};
        this.arrOne.forEach(item => {
            if (objForAllCategories.hasOwnProperty(item.category))
                objForAllCategories[item.category] = objForAllCategories[item.category] + item.series_1;
            else
                objForAllCategories[item.category] = item.series_1;
        });
        for (var prop in objForAllCategories) {
            this.allCategoriesAndValues.push({ 
                category: prop, 
                series_1: objForAllCategories[prop] 
            });
        }

ดังนั้นมันจะมีผลอยู่ใน:

allCategoriesAndValues = [
    {
        "category": "Category 1",
        "series_1": 57       // 25 + 32 adding up series_1 from all 'Category 1' items in arrOne
    },
    {
        "category": "Category 2",
        "series_1": 11      // only 1 'Category 2' from arrOne
    },
    {
        "category": "Category 3",
        "series_1": 120     // 74 + 46 adding up series_1 from all 'Category 3' items in arrOne
    }
]

อย่างไรก็ตามฉันต้องการที่จะสามารถเพื่อเพิ่มไม่ใช่แค่ series_1 แต่ยังรายการอื่นทั้งหมด.

นี่ตัวอย่างเช่นเดียวที่มีหมวดหมู่และ series_1 และ series_2 เป็นกุญแจ อย่างไรก็ตามมันอาจจะยังมี:

  1. series_3
  2. series_4
  3. series_5
  4. series_6
  5. series_7
  6. etc..

ยังไงฉันสามารถบัญชีผู้ใช้สำหรับศักยภาพ series_x?

แรงผล:

allCategoriesAndValues = [
    {
        "category": "Category 1",
        "series_1": 57,
        "series_2": 124,
        ..... if 'series_3', 'series_4' etc. existed, it would be included in this as above
    },
    {
        "category": "Category 2",
        "series_1": 11,
        "series_2": 22,
        ..... if 'series_3', 'series_4' etc. existed, it would be included in this as above
    },
    {
        "category": "Category 3",
        "series_1": 120,
        "series_2": 127,
        ..... if 'series_3', 'series_4' etc. existed, it would be included in this as above
    }
]
arrays javascript json key-value
2021-11-24 02:19:06
6

คำตอบที่ดีที่สุด

2

เพื่อรับมือกับการคุณสมบัติของหลายเหตุผลคุณสามารถเล่นกันและทรัพย์สินแล้วเช็คว่ามันตรงกับ regex series_\d+. ถ้ามันทำคุณรู้ว่ามันเป็นทรัพย์สินที่คุณต้อง increment และจัดการได้ตามคาด(เป็นทรัพย์สินตัวตนขอดูเป็นยังจำเป็นต้องอย่างที่ชี้ให้เห็นโดย Jayce444).

ต่อไปนี้ใช้ทางออก Array.reduce. ใน reducer ฟังก์ชันมัเช็คว่า accumulator อาเรย์มีข้อรายการเดียวกัน category ทรัพย์สินเป็นคนปัจจุบันโดนดึงเข้ามา ถ้าเป็นแบบนั้นมันจะ increment ที่เหมาะสมคุณสมบัติของ. ไม่อย่างนั้นมันจะผลักปัจจุบันรายการที่ accumulator อาเรย์.

arrOne=[{series_1:25,category:"Category 1",series_2:50},{series_1:11,category:"Category 2",series_2:22},{series_1:32,category:"Category 1",series_2:74},{series_1:74,category:"Category 3",series_2:98},{series_1:46,category:"Category 3",series_2:29,series_3:50}];

const res = arrOne.reduce((a, b) => {
  let found = a.find(e => e.category == b.category)
  if (found) {
    Object.keys(b).forEach(e => {
      if (/series_\d+/g.test(e)) found[e] = found[e] ? found[e] + b[e] : b[e];
    })
  } else {
    a.push(b)
  }
  return a;
}, [])

console.log(res)

2021-11-24 02:38:52

นี้วิธีทำลายเมื่อทุกอย่างวัตถุไม่มีคนเดียวกับชุดกุญแจ อย่างเช่นถ้าคุณเพิ่ม series_3: 5 ก่อนสิ่งเดียวที่มันต้องเป็น series_3: NaN ในผลลัพธ์.
Jayce444

ไม่นี่คิดถึงคนใหม่ ชุด ปุ่มเพิ่มสำหรับผู้หมวดหมู่เดียวกันในทีหลังวัตถุ? มันเป็น โอ(มะ^2) (ฉันคิดว่า)
Phil

@ฟิลขอบคุณที่สังเกตเห็น ฉันปรับคำตอบของฉันแล้ว
Spectric
1

บางอย่างเหมือนจะทำงานแล้ว

arrOne = [ { "series_1": 25, "category": "Category 1", "series_2": 50 }, { "series_1": 11, "category": "Category 2", "series_2": 22 }, { "series_1": 32, "category": "Category 1", "series_2": 74 }, { "series_1": 74, "category": "Category 3", "series_2": 98 }, { "series_1": 46, "category": "Category 3", "series_2": 29 },];

const result = [];
arrOne.reduce((acc, {category, ...series}) => {
  if (acc.has(category)) {
    Object.entries(series).forEach(([key, value]) => {
      if (key.startsWith('series_')) {
        acc.get(category)[key] = (acc.get(category)[key] || 0) + value;
      }
    });
  } else {
    const item = {category, ...series};
    result.push(item);
    acc.set(category, item);
  }
  return acc;
}, new Map());

console.log(result);

2021-11-24 02:27:40

ไม่ค่อยแน่ใจเกี่ยวกับเรื่องเป็น reducer กับผลข้างเคียงคง(result การกลายพันธุ์)
Phil
0

สร้างแผนที่ที่ต้องลำดับหน้าชุดดันโดยหมวดหมู่.

งั้นสร้างตารางคู่ลำดับจากนั้นแผนที่พร้อมกับคาบกุญแจมือ category

const arr1 = [{"series_1":25,"category":"Category 1","series_2":50},{"series_1":11,"category":"Category 2","series_2":22},{"series_1":32,"category":"Category 1","series_2":74},{"series_1":74,"category":"Category 3","series_2":98},{"series_1":46,"category":"Category 3","series_2":29}]

const t1 = performance.now()

const cats = arr1.reduce((map, { category, ...series }) =>
  map.set(category, Object.entries(series)
    .reduce((s, [ key, count ]) => ({
      ...s,
      [ key ]: (s[key] ?? 0) + count
    }), map.get(category) ?? {})
  ), new Map())

const allCategoriesAndValues = Array.from(cats, ([ category, series ]) => ({
  category,
  ...series
}))

const t2 = performance.now()

console.info(allCategoriesAndValues)
console.log(`Took ${t2 - t1}ms`)
.as-console-wrapper { max-height: 100% !important; }

2021-11-24 02:32:20
0

ฉันจะทำอย่างนั้นทางนี้...

const arrOne = 
  [ { series_1: 25, category: 'Category 1', series_2: 50 } 
  , { series_1: 11, category: 'Category 2', series_2: 22 } 
  , { series_1: 32, category: 'Category 1', series_2: 74 } 
  , { series_1: 74, category: 'Category 3', series_2: 98 } 
  , { series_1: 46, category: 'Category 3', series_2: 29 } 
  ] 

console.time('chrono')

const allCategoriesAndValues =
  Object.entries(
  arrOne.reduce((r,{ category, ...series })=>
    {
    let cat = r[category] = r[category] ?? {} 
    Object.entries(series).forEach(([sName,val]) => cat[sName] = (cat[sName] ?? 0) + val);
    return r
    },{})
  ).map(([category,series])=>({category,...series}))

console.timeEnd('chrono')

console.log( allCategoriesAndValues )
.as-console-wrapper {max-height: 100%!important;top:0 }

2021-11-24 02:47:52
0

คุณสามารถ iterate นือตารางคู่ลำดับของวัตถุแล้วกุญแจของแต่ละวัตถุ,จัดเก็บไปเป็นแค่เพื่อวัตถุ. คุณแค่ต้องเหมือนกันเช็คสำหรับการดำรงอยู่ของกันและกุญแจและเพิ่มถ้ามันหายไปหรือคุณแค่ coalesce falsey กุญแจให้เป็นค่าปริยายอย่างที่ฉันทำ ฉันเอาหมวดหมู่ญแจจากวัตถุหลังจากที่ผมให้คุณค่าของมัดังนั้นฉันไม่ต้องพยายามที่จะข้ามมันตอนกุญแจสำคัญทำซ้ำ.

const arrOne = [
  {"series_1": 25, "category": "Category 1", "series_2": 50},
  {"series_1": 11, "category": "Category 2", "series_2": 22},
  {"series_1": 32, "category": "Category 1", "series_2": 74},
  {"series_1": 74, "category": "Category 3", "series_2": 98},
  {"series_1": 46, "category": "Category 3", "series_2": 29},
];

let buffer = {};
arrOne.forEach(i=>{
  let c = i.category;
  buffer[c] = buffer[c] || {};
  delete i.category;
  Object.keys(i).forEach(k=>{
    buffer[c][k] = buffer[c][k] || 0;
    buffer[c][k] += i[k];
  });
});

console.log(buffer);

let final = Object.keys(buffer).map(k=>{return {[k]: buffer[k]}});
console.log(final);

ถ้าคุณไม่ต้องการเรื่องนี้อยู่ในตารางคู่ลำดับ,สุดท้ายขั้นคือเลือกหรอกนะ มันเดียวที่อยู่ก่อนเพื่อเปลี่ยนที่วัตถุไปเป็นอาเรย์.

2021-11-24 02:31:18
0

นี่คือวิธีที่ฉันจะทำมัน

const res = arrOne.reduce((acc, { category, ...vals }) => {
    if (acc[category]) {
        Object.entries(vals).forEach(([ key, val ]) => acc[category][key] = acc[category][key] ? acc[category][key] + val : val);

    } else {
        acc[category] = vals;

    }

    return acc;
}, {});
2021-11-24 03:11:13

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่