简体   繁体   English

带有特定字符串的切片数组

[英]slice array with a specific string

How do I split an array of arrays by searching for a specific string?如何通过搜索特定字符串来拆分 arrays 的数组? 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.我想在每个 object 随机位置的特定字符串处拆分字符串。我想删除“特定字符串”和“特定字符串”数组之后的所有内容。

I have arrays that look like this我有 arrays 看起来像这样

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我希望我的 output 数组看起来像这样

[{
    "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我知道 slice 不适用于字符串,但这是我想用代码实现的

for(var i=0, len = array.length; i < len; i++){
array[0].arr['categories'].slice(0,'specific string');
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM