-1

Sorry I am new to Javascript.

I have the following var array ...

    var sections = [
        { key: "SB04", label:"Storage 4 WBA" },
        { key: "SBO4", label:"Storage 4 CT" },
        { key: "CR04", label:"Storage 4 CR" },
    ];

How can I re-define this array to return the same label once, based on a key that matches the ones in the list, as shown here?

So I've tried to use this version, but it brings back syntax error in Chrome.

    var sections = [
        { key: ("SB04","SB04A","SB04B"), label:"Storage 4 WBA" },
        { key: ("SBO4","SBO4C","SBO4D","SBO4TEST"), label:"Storage 4 CT" },
        { key: "CR04", label:"Storage 4 CR" },
    ];

So something like GetLabel("SB04C") or GetLabel("SB04TEST"), will return "Storage 4 CT" only.

Obviously, the GetLabel() has to be defined. But I am happy just to get an answer on how to re-code the above var into something that is understood by the browser.

I've tried this but my use of it returns the label more than once...

    var sections = [
        { key: "SB04", label:"Storage 4 WBA" },
        { key: "SB04A", label:"Storage 4 WBA" },
        { key: "SB04B", label:"Storage 4 WBA" },
        { key: "SBO4", label:"Storage 4 CT" },
        { key: "SBO4C", label:"Storage 4 CT" },
        { key: "SBO4D", label:"Storage 4 CT" },
        { key: "SBO4TEST", label:"Storage 4 CT" },
        { key: "CR04", label:"Storage 4 CR" },
    ];

Output for example:

> Storage 4 WBA
> Storage 4 WBA
> Storage 4 WBA

even though I've given my code only a single key - SB04A

Fandango68
  • 4,461
  • 4
  • 39
  • 74
  • Javascript is a dynamically typed language. It looks like you are impying the types of keys it can have, but this is not how it works. See: https://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages. If you use typescript you can use types but even then it can still compile to javascript without limiting the array. It merely warns you via editor like vscode or command line when running through compiler if it is wrong. – Steve Tomlin Aug 10 '21 at 06:37
  • What does _"it returns the label more than once"_ mean? How are you using `sections`? What value do you expect to get? What value are you actually getting? – Phil Aug 10 '21 at 06:43
  • @Phil Updated with more details. – Fandango68 Aug 11 '21 at 01:01

2 Answers2

1

Would this be what you mean?

const sections = getValues()
  .reduce((acc, val) => ({ ...acc,
    [val.label]: (acc[val.label] || []).concat(val.key)
  }), {});

console.log(sections);

// this can be mapped back to an array of objects:
const mapped = Object.entries(sections)
  .map( ([key, value]) => ({label: key, keys: value}) ) ;
console.log(mapped[0]);

function getValues() {
  return [{
      key: "SB04",
      label: "Storage 4 WBA"
    },
    {
      key: "SB04A",
      label: "Storage 4 WBA"
    },
    {
      key: "SB04B",
      label: "Storage 4 WBA"
    },
    {
      key: "SBO4",
      label: "Storage 4 CT"
    },
    {
      key: "SBO4C",
      label: "Storage 4 CT"
    },
    {
      key: "SBO4D",
      label: "Storage 4 CT"
    },
    {
      key: "SBO4TEST",
      label: "Storage 4 CT"
    },
    {
      key: "CR04",
      label: "Storage 4 CR"
    },
  ];
}
KooiInc
  • 119,216
  • 31
  • 141
  • 177
1

I think you are looking for Set.

const sections = [
  { key: new Set(["SB04", "SB04A", "SB04B"]), label: "Storage 4 WBA" },
  {
    key: new Set(["SBO4", "SBO4C", "SBO4D", "SBO4TEST"]),
    label: "Storage 4 CT",
  },
  { key: new Set(["CR04"]), label: "Storage 4 CR" },
];

const getLabelByKey = (sections, key) => {
  for (const section of sections) {
    if (section.key.has(key)) {
      return section.label;
    }
  }
  return null;
};

console.log(getLabelByKey(sections, "SBO4C"));
ikhvjs
  • 5,316
  • 2
  • 13
  • 36