Home > Back-end >  How to remove strings which extends original string from JavaScript array?
How to remove strings which extends original string from JavaScript array?

Time:06-08

I have the following array: ['total.color.violet', 'total.color.violet.truefont', 'total.color.red', 'total.color.red.circle', 'total.color.red.circle.elefant', 'total.color.blue', 'total.color.yellow', 'total.color.yellow.dotted']. As we can see all the strings have constant part total.color and the variable part. I need to get the next result: ['total.color.violet', 'total.color.red', 'total.color.blue', 'total.color.yellow'] - to remove strings which have more than 'third level' complexity and leave string only with 'second level' complexity. Please, look throw this algorithm and give me any tips, if it is possible.

CodePudding user response:

The easiest method would be use filter based on the length of calling split on each word. javascript split

One easy method would be to loop through the array and remove the "total.color." portion from each string using slicing or the replace method. Then, on a second loop through, if the string contains a "." you know there are more than 3 levels. See : String Replace

Sting includes

A more complicated method would be to use a regex

CodePudding user response:

Here you go. Just use 'level1.level2.level3'.split('.') to break it into an array that is separated by ..

let values = ['total.color.violet', 'total.color.violet.truefont', 'total.color.red', 'total.color.red.circle', 'total.color.red.circle.elefant', 'total.color.blue', 'total.color.yellow', 'total.color.yellow.dotted'];

let level123 = [];
let colorsOnly = [];
for(let value of values){
    // Get each level (seperated by '.')
    let level = value.split('.');
    
    // Add each level (1-3)
    level123.push(`${level[0]}.${level[1]}.${level[2]}`);
    
    // Add only the colors
    colorsOnly.push(level[2]);
}

// Show the newly formatted values
console.log("Levels 1-3: ", level123);
console.log("Colors only: ", colorsOnly);

CodePudding user response:

A relatively simple case of 'count the dots'.

Here I'm using a regular expression to determine the number of occurrences of . within the string. 'g' flag returns all occurrences in the string.

match can return null in the case of no matches, so the || [] ensures that there is an array of zero length, so that we can access the property length.

All entries with 2 or less dots is retained in the filtered array.

let values = ['total.color.violet', 'total.color.violet.truefont', 'total.color.red', 'total.color.red.circle', 'total.color.red.circle.elefant', 'total.color.blue', 'total.color.yellow', 'total.color.yellow.dotted']

const output = values
    .filter(value => (value.match(/\./g) || []).length <= 2)

console.log(output)

CodePudding user response:

Adding the comment is the only option for me, so: I have an ['total.color.violet', 'total.color.violet.truefont', 'total.color.red', 'total.color.red.circle', 'total.color.red.circle.elefant', 'total.color.blue', 'total.color.yellow', 'total.color.yellow.dotted'] array. I have this array and 'total.color' prefix as two input parameters. Desired result is: ['total.color.violet', 'total.color.red', 'total.color.blue', 'total.color.yellow'] - it is an array which contains strings which only have prefix third level of complexity ('total.color.blue') and so on.

Also the input parameters can be: ['telex.fast', 'telex.fast.line.hope', 'total.fast.ring', 'telex.slow', 'total.slow.motion'] and 'telex' prefix. The result could be ['telex.fast', 'telex.slow'].

I've tried to use smth like string.search/includes and looping.

  • Related