1

How do I split an array of arrays by searching for a specific string? I have an array of hundreds of objects. I want to split the string at the specific string which is at a random place for each object. I want to remove everything that comes after the 'specific string' and the 'specific string' array.

I have arrays that look like this

array = 
[{
    "post": 1,
    "arr": 
        {
            "categories":
            [
                [
                    "string 1"
                ],
                [
                    "string 2"
                ],
                [
                    "string 3"
                ],
                [
                    "specific string"
                ],
                [
                    "string 4"
                ],
                [
                    "string 5"
                ],
                [
                    "string 6"
                ],
                [
                    "string 7"
                ]
            ]
        }
},
{
    "post": 3,
    "arr": 
    {
        "categories":
        [
            [
                "string 1"
            ],
            [
                "string 2"
            ],
            [
                "specific string"
            ],
            [
                "string 3"
            ],
            [
                "string 4"
            ],
            [
                "string 5"
            ],
            [
                "string 6"
            ],
            [
                "string 7"
            ]
        ]
    }
},


{
    "post": 2,
    "arr": 
    {
        "categories":
        [
            [
                "string 1"
            ],
            [
                "string 2"
            ],
            [
                "string 3"
            ],
            [
                "string 4"
            ],
            [
                "string 5"
            ],
            [
                "specific string"
            ],
            [
                "string 6"
            ],
            [
                "string 7"
            ],
            [
                "string 8"
            ]
        ]
    }
}
]

i want my output array to look like this

[{
    "post": 1,
    "arr": 
        {
            "categories":
            [
                [
                    "string 1"
                ],
                [
                    "string 2"
                ],
                [
                    "string 3"
                ]
            ]
        }
},
{
    "post": 3,
    "arr": 
    {
        "categories":
        [
            [
                "string 1"
            ],
            [
                "string 2"
            ]
        ]
    }
},


{
    "post": 2,
    "arr": 
    {
        "categories":
        [
            [
                "string 1"
            ],
            [
                "string 2"
            ],
            [
                "string 3"
            ],
            [
                "string 4"
            ],
            [
                "string 5"
            ]
        ]
    }
}
]

I know slice doesnt work with strings but this is kind of what I wanted to achieve with code

for(var i=0, len = array.length; i < len; i++){
array[0].arr['categories'].slice(0,'specific string');
}
2
  • btw, why do you have arrays with only a single value? Commented Nov 22, 2022 at 18:54
  • that is how I need it to be formatted for an import @NinaScholz Commented Nov 22, 2022 at 19:33

1 Answer 1

1

You could find the index of wanted array and slice the array.

const
    array = [{ post: 1, arr: { categories: [["string 1"], ["string 2"], ["string 3"], ["specific string"], ["string 4"], ["string 5"], ["string 6"], ["string 7"]] } }, { post: 3, arr: { categories: [["string 1"], ["string 2"], ["specific string"], ["string 3"], ["string 4"], ["string 5"], ["string 6"], ["string 7"]] } }, { post: 2, arr: { categories: [["string 1"], ["string 2"], ["string 3"], ["string 4"], ["string 5"], ["specific string"], ["string 6"], ["string 7"], ["string 8"]] } }],
    result = array.map(({ arr: { categories }, ...o }) => ({
        ...o,
        arr: { categories: categories.slice(0, categories.findIndex(([s]) => s === "specific string")) }
    }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

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.