Mam listę obiektów, które wyglądają tak: [ { "id": 1, "ifsG0": "1-G01", "price": 200, ... }, { "id": 2, "ifsG0" : "1-G01&...

1
Quentin 23 czerwiec 2021, 10:09

5 odpowiedzi

Najlepsza odpowiedź

Korzystanie z reduce i flatmap

var list = [
    { "id": 1, "ifsG0": "1-G01", "price": 200 },
    { "id": 2, "ifsG0": "1-G01", "price": 200 },
    { "id": 3, "ifsG0": "1-G02", "price": 140 },
    { "id": 4, "ifsG0": "1-G03", "price": 90 },
    { "id": 5, "ifsG0": "1-G01", "price": 200 },
    { "id": 6, "ifsG0": "1-G03", "price": 90 },
    { "id": 7, "ifsG0": "1-G02", "price": 140 },
    { "id": 8, "ifsG0": "1-G01", "price": 200 },
    { "id": 9, "ifsG0": "1-G01", "price": 200 },
    { "id": 10, "ifsG0": "1-G03", "price": 90 },
    { "id": 11, "ifsG0": "1-G03", "price": 90 },
    { "id": 12, "ifsG0": "1-G02", "price": 140 },
];

var ifs = list.reduce((a, l) => {
  if (!a[l.ifsG0]) a[l.ifsG0] = { p: l.price, q: 0 }
  a[l.ifsG0].q++
  return a
}, {})

var partNo = Object.keys(ifs).sort().flatMap(i => [i, ifs[i].q, ifs[i].p]).join('#')

console.log(partNo);
3
User863 23 czerwiec 2021, 07:33

To jest po prostu „grupuj według” mapowane na ciąg. Wykonanie tego w jednej linii niekoniecznie jest lepsze, ale możesz uprościć to do pojedynczego reduce(), przenieść wywołanie sort() do po, aby było bardziej wydajne, a następnie flatMap() i join() posortowana tablica.

const
  list = [{ "id": 2, "ifsG0": "1-G01", "price": 200 }, { "id": 1, "ifsG0": "1-G01", "price": 200 }, { "id": 3, "ifsG0": "1-G02", "price": 140 }, { "id": 4, "ifsG0": "1-G03", "price": 90 }, { "id": 5, "ifsG0": "1-G01", "price": 200 }, { "id": 6, "ifsG0": "1-G03", "price": 90 }, { "id": 7, "ifsG0": "1-G02", "price": 140 }, { "id": 8, "ifsG0": "1-G01", "price": 200 }, { "id": 9, "ifsG0": "1-G01", "price": 200 }, { "id": 10, "ifsG0": "1-G03", "price": 90 }, { "id": 11, "ifsG0": "1-G03", "price": 90 }, { "id": 12, "ifsG0": "1-G02", "price": 140 },],

  partNo = Object
    .values(
      list.reduce((a, { ifsG0, price }) => (
        (a[ifsG0] ??= { ifsG0, price, count: 0, }).count += 1, a), {})
    )
    .sort((a, b) => a.ifsG0.localeCompare(b.ifsG0))
    .flatMap(({ ifsG0, count, price }) => [ifsG0, count, price])
    .join('#');

console.log(partNo);
2
pilchard 23 czerwiec 2021, 07:48

Możesz użyć reducei Map Object, aby uzyskać ilość. Następnie użyj prostego sortowania i mapowania, aby uzyskać wynik.

const list = [
  { id: 11, ifsG0: "1-G03", price: 90 },
  { id: 1, ifsG0: "1-G01", price: 200 },
  { id: 2, ifsG0: "1-G01", price: 200 },
  { id: 3, ifsG0: "1-G02", price: 140 },
  { id: 4, ifsG0: "1-G03", price: 90 },
  { id: 5, ifsG0: "1-G01", price: 200 },
  { id: 6, ifsG0: "1-G03", price: 90 },
  { id: 7, ifsG0: "1-G02", price: 140 },
  { id: 8, ifsG0: "1-G01", price: 200 },
  { id: 9, ifsG0: "1-G01", price: 200 },
  { id: 10, ifsG0: "1-G03", price: 90 },
  { id: 12, ifsG0: "1-G02", price: 140 },
];

const output = [ ...list.reduce( (acc, e) =>
      acc.set(e.ifsG0, { ...e, qty: (acc.get(e.ifsG0)?.qty || 0) + 1 }),
    new Map()
    )]
  .sort()
  .map(s => `${s[1].ifsG0}#${s[1].qty}#${s[1].price}`)
  .join("#");

console.log(output);
2
ikhvjs 23 czerwiec 2021, 08:05

Jeszcze jeden sposób, aby to zrobić, ponownie używając Array.reduce i trochę destrukturyzowania, opcjonalnego tworzenia łańcuchów (?.), Następnie Array.flat() i Array.join() oraz Object.values:

const list = [
    { "id": 1, "ifsG0": "1-G01", "price": 200 },
    { "id": 2, "ifsG0": "1-G01", "price": 200 },
    { "id": 3, "ifsG0": "1-G02", "price": 140 },
    { "id": 4, "ifsG0": "1-G03", "price": 90 },
    { "id": 5, "ifsG0": "1-G01", "price": 200 },
    { "id": 6, "ifsG0": "1-G03", "price": 90 },
    { "id": 7, "ifsG0": "1-G02", "price": 140 },
    { "id": 8, "ifsG0": "1-G01", "price": 200 },
    { "id": 9, "ifsG0": "1-G01", "price": 200 },
    { "id": 10, "ifsG0": "1-G03", "price": 90 },
    { "id": 11, "ifsG0": "1-G03", "price": 90 },
    { "id": 12, "ifsG0": "1-G02", "price": 140 },
];

const result = Object.values(list.reduce((acc, { ifsG0, price } ) => { 
    return { ...acc, [ifsG0]: [ifsG0, ((acc?.[ifsG0]?.[1]) || 0) + 1 , price ]};
}, {})).flat().join("#")

console.log(result);
1
Terry Lennox 23 czerwiec 2021, 07:51

Oto reduce, który śledzi ilość i maps do pożądanego wyniku.

const partNo = Object.values(list.reduce((b, a) => {
   if (b.hasOwnProperty(a.ifsG0)) b[a.ifsG0].quantity++;
   else b[a.ifsG0] = {...a, quantity: 1};
   return b; }, {}))
  .sort((a,b)=>a.ifsG0.localeCompare(b.ifsG0))
  .map(e => [e.ifsG0, e.quantity, e.price].join('#')).join('#')
var list = [{ "id": 1, "ifsG0": "1-G01", "price": 200 },
    { "id": 2, "ifsG0": "1-G01", "price": 200 },
    { "id": 3, "ifsG0": "1-G02", "price": 140 },
    { "id": 4, "ifsG0": "1-G03", "price": 90 },
    { "id": 5, "ifsG0": "1-G01", "price": 200 },
    { "id": 6, "ifsG0": "1-G03", "price": 90 },
    { "id": 7, "ifsG0": "1-G02", "price": 140 },
    { "id": 8, "ifsG0": "1-G01", "price": 200 },
    { "id": 9, "ifsG0": "1-G01", "price": 200 },
    { "id": 10, "ifsG0": "1-G03", "price": 90 },
    { "id": 11, "ifsG0": "1-G03", "price": 90 },
    { "id": 12, "ifsG0": "1-G02", "price": 140 }];

const partNo = Object.values(list.reduce((b,a)=>{ if (b.hasOwnProperty(a.ifsG0)) b[a.ifsG0].quantity++; else b[a.ifsG0] = {...a, quantity: 1}; return b; },{}))
  .sort((a,b)=>a.ifsG0.localeCompare(b.ifsG0))
  .map(e=>[e.ifsG0, e.quantity, e.price].join('#')).join('#')

console.log(partNo);
console.log("1-G01#5#200#1-G02#3#140#1-G03#4#90 -- desired")
1
Kinglish 23 czerwiec 2021, 07:57