Home > Software engineering >  slice array with a specific string
slice array with a specific string

Time:11-23

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

CodePudding user response:

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

  • Related