'Argument of type 'unknown' is not assignable to parameter of type 'string'." When I try to parse a stringified array [duplicate]

Sorry if this is a duplicate. I'm looking for the best way to convert an array into a key I can use for a set in JavaScript.

const set = new Set()
const arr = [1,0,-1]
const arr2 = [1,0,-1]
set.add(arr)
set.add(arr2)

This should work because both arr and arr2 are different objects and the set recognizes their references as unique. What's the best way I can convert arr and arr2 into keys so the set recognizes them as unique?

I tried doing JSON.stringify(arr), but I had difficulty converting them back into number[].

Any help is appreciated!

Here is the full version of the code I'm trying:

function twoSum(nums: number[], excludeIndex: number, target: number)
{
    let startPointer = 0;
    let endPointer = nums.length - 1;
    while(startPointer < endPointer)
    {
        if(startPointer === excludeIndex)
        {
            startPointer++;
        }
        if(endPointer === excludeIndex)
        {
            endPointer--;
        }
        if(nums[startPointer] + nums[endPointer] === target)
        {
            return [startPointer, endPointer]
        }
        if(nums[startPointer] + nums[endPointer] > target)
        {
            endPointer--;
        }
        else{
            startPointer++;
        }
    }
    return undefined
}

function threeSum(nums: number[]): number[][] {
    if(nums.length < 3)
    {
        return []
    }
    nums.sort((a,b) => a - b);
    let index = 0;
    const uniqueTriplets = new Set()
    while(index < nums.length)
    {
        const num = nums[index]
        const twoSumResult = twoSum(nums, index, (num * -1)) 
        if(twoSumResult !== undefined)
        {
            const triplet = [num, nums[twoSumResult[0]], nums[twoSumResult[1]]]
            triplet.sort((a,b) => a - b)
            uniqueTriplets.add(JSON.stringify(triplet))
        }
        index++;
    }

    return Array.from(uniqueTriplets.keys()).map(k => JSON.parse(k))
}

On the last return statement I get an error saying: "Argument of type 'unknown' is not assignable to parameter of type 'string'." When I add the JSON.parse(k)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source