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

7
  • 2
    What specific issue are you having? Commented Feb 23, 2023 at 17:31
  • If the input is [ "1", "2|5", "1|2", "2" ], is the output [ { stdSizeCode: [ 2 ], productHierSk: 1 }, { stdSizeCode: [ 5 ], productHierSk: 2 } ]? Commented Feb 23, 2023 at 17:34
  • Hi @sebastian Simon : under unique product id different size codes as expected in an description Commented Feb 23, 2023 at 17:34
  • @SivaShanker Please edit your question. Commented Feb 23, 2023 at 17:34
  • object is an array, Commented Feb 23, 2023 at 17:36

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

There is a typo. I think it should be (code !== undefined)
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);

1 Comment

According to the OP, 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.
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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.