0

I have a script that pulls responseText from a website and parses it into JSON. An example of the output looks like this:

ctiResponse = [
    "Category / Type1 / Item1",
    "Category / Type1 / Item2",
    "Category / Type1 / Item2",
    "Category / Type1 / Item3",
    "Category / Type2 / Item1",
    "Category / Type2 / Item2",
    "Category / Type2 / Item2",
    "Category / Type2 / Item3",
    "Category / Type3 / Item1",
    "Category / Type3 / Item2",
    "Category / Type3 / Item2",
    "Category / Type3 / Item3",
];

I think the .map() function is what I need to use to accomplish my end result, but I'm not sure how to go about it. I'd like to create another array from the parsed JSON response that would end up like the following. Note that the parsed JSON response has repeated Items, and I only need those once in the second array.

let ctis = [
    {
        "Category": [
            "Type1": {"Item1", "Item2", "Item3"},
            "Type2": {"Item1", "Item2", "Item3"},
            "Type3": {"Item1", "Item2", "Item3"},
            ],
    }
]
4
  • "parses it into JSON" You deserialise/parse JSON in to a JavaScript object and serialise/stringify an object to JSON. It looks like you have a JavaScript array and therefore the question doesn't appear to be JSON related. Commented Aug 5, 2022 at 12:44
  • You desired result, does the list of items has to be an object or can it be an array? Commented Aug 5, 2022 at 12:45
  • An array works. Commented Aug 5, 2022 at 12:46
  • 1
    Your desired result is not valid - array items do not have property names ("Type1", "Type2" etc.) Commented Aug 5, 2022 at 12:47

1 Answer 1

1

const ctiResponse = [
    "Category / Type1 / Item1",
    "Category / Type1 / Item2",
    "Category / Type1 / Item2",
    "Category / Type1 / Item3",
    "Category / Type2 / Item1",
    "Category / Type2 / Item2",
    "Category / Type2 / Item2",
    "Category / Type2 / Item3",
    "Category / Type3 / Item1",
    "Category / Type3 / Item2",
    "Category / Type3 / Item2",
    "Category / Type3 / Item3",
];

console.log(
  ctiResponse.reduce(
    (obj, line) => {
      const [category, type, item] = line.split(' / ');
      obj[category] = {...obj[category]}
      obj[category][type] = [...new Set([...obj[category][type] || [], item])];
        return obj;
    }
  , {}) 
)

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

3 Comments

This gets me where I want to go, however, I don't want duplicate Items in the Type arrays.
obj[category][type] = [...new Set([...obj[category][type], item])]; add some thing like this. Set will remove duplicates
obj[category][type] = [...new Set([...obj[category][type] || [], item])]; works. Thank you @mateusz-rorat

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.