I have an array like this:
array[
{name:test},
{name:test1},
{name:test2}
]
I have the str and n variables
let str = 'test';
let n = 1;
let obj = array.some(o => o.name.includes(str + n));
//check if string exist in array + n
if (obj === true) ++n
else str += n
console.log(str) // 'test'
the idea is that I want to check if string(test) exist in array of objects, if it exists I should add number, if it does not exist, then I should not add number
what i should get:
if let str = "test" - console.log(str) // 'test3'
if let str = "test2" - console.log(str) // 'test2_1'
I just try to use:
for (i = 0; i < array.length; i++) {
let obj = array[i].name.includes(str + n)
while (obj === true) n++
str += n
}
console.log(str) // test111