2

I have a dynamic object something like this

var object = [
    "70669",
    "70669|9436",
    "70669|8353",
    "70669|8914",
    "70669|9522",
    "70669|8422",
    "70669|9639"
    ]

My expected output is something like this

[{
    stdSizeCode: [9436, 8353, 8914, 9522, 8422, 9639]
    productHierSk: 70669
}]

I have tried to looking into this link and did not work the way that is expected. Can someone please suggest

SivaShanker
  • 87
  • 1
  • 9

3 Answers3

1

You could group by with an object and get the values as result.

const
    data = ["70669", "70669|9436", "70669|8353", "70669|8914", "70669|9522", "70669|8422", "70669|9639"],
    result = Object.values(data.reduce((r, s) => {
        const [productHierSk, code] = s.split('|').map(Number);
        r[productHierSk] ??= { stdSizeCode: [], productHierSk };
        if (code !== undefined) r[productHierSk].stdSizeCode.push(code);
        return r;
    }, []));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

If I understand your question correctly

It work fine :

var object = [
  "70669",
  "70669|9436",
  "70669|8353",
  "70669|8914",
  "70669|9522",
  "70669|8422",
  "70669|9639",
];

var result = [{ stdSizeCode: [], productHierSk: null }];

object.map((item) => {
  if (item.includes("|")) {
    result[0].stdSizeCode.push(parseInt(item.split("|")[1]));
  } else {
    result[0].productHierSk = parseInt(item);
  }
});

console.log("result : ", result);
  • [According to the OP](/questions/75548301/grouping-a-dynamic-array-object-and-forming-a-object-required/75548589#comment133290417_75548301), the input `[ "1", "2|5", "1|2", "2" ]` shall provide the output `[ { stdSizeCode: [ 2 ], productHierSk: 1 }, { stdSizeCode: [ 5 ], productHierSk: 2 } ]`. Your code assumes, there will only be a single distinct `productHierSk`, which is incorrect. – Sebastian Simon Feb 23 '23 at 18:02
0

Use split together with the destructuring assignment to get both parts of each string. Use map with Number to convert each substring to a number. See How to convert all elements in an array to integer in JavaScript?.

The reduce call will iterate over your array and merge everything into a new Map object. The Map makes it easier to find and match your productHierSks. If you care about the order in which different productHierSks occur in your object, you should use a Map instead of a plain object.

With entries and Array.from the Map is transformed into an Array of [ key , value ] pairs (in this case, the key is productHierSk and the value is the Array stdSizeCode). One more map transforms each of these into the final object.

const object1 = [
    "70669",
    "70669|9436",
    "70669|8353",
    "70669|8914",
    "70669|9522",
    "70669|8422",
    "70669|9639"
  ],
  object2 = [
    "1",
    "2|5",
    "1|2",
    "2"
  ];

const getSizeCodes = (object) => Array.from(object.reduce((stdSizeCodes, string) => {
    const [
        productHierSk,
        stdSizeCode
      ] = string.split("|").map(Number);

    if(!stdSizeCodes.has(productHierSk)){
      stdSizeCodes.set(productHierSk, []);
    }

    if(stdSizeCode){
      stdSizeCodes.get(productHierSk).push(stdSizeCode);
    }

    return stdSizeCodes;
  }, new Map()).entries())
    .map(([ productHierSk, stdSizeCode ]) => ({
      productHierSk,
      stdSizeCode
    }));

console.log(getSizeCodes(object1));
console.log(getSizeCodes(object2));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75