-1

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
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
risky
  • 109
  • 2
  • 11
  • 1
    How do you get from `"test2"` to `"test2_1"`? – Lain Mar 18 '22 at 07:39
  • *"if not exist not add number"* But you've added `_1` in that example...? – T.J. Crowder Mar 18 '22 at 07:39
  • What purpose is the `while` loop supposed to serve? (It'll just lock things up the first time it's `true`, since `obj` never changes. And `obj` is a very strange name for a variable that will contain a boolean, not an object.) Just do a simple loop (`for-of` would be my choice, but [you have options](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript)) and, if `element.name` includes the relevant text, make the relevant change to it. No need for inner loops, `some` isn't useful here unless you want to stop early (and even then, you can `break` a loop), ... – T.J. Crowder Mar 18 '22 at 07:41
  • @T.J.Crowder i' va added _1 cause test2 exist in array – risky Mar 18 '22 at 07:43
  • @risky - You've said "not add number" and then added a number. But never mind, just use a loop as described above and you should be able to get the task done, whatever it is. – T.J. Crowder Mar 18 '22 at 07:48

2 Answers2

1

You can use .filter()

let array = [
    {name:'test'},
    {name:'test1'},
    {name:'test2'}
];
let str = 'test', currentStr = 'test';
let n = 1;

if (array.filter((item) => item.name === str).length) {
    currentStr = str + n++;
}

while (array.filter((item) => item.name === currentStr).length) {
    currentStr = str + n++;
}
 
console.log(currentStr);
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • thanks for answer, but i should check all objects values and in your code i should get console.log(str) // test3 - the name must be uniqe let array = [ {name:'test'}, {name:'test_1'}, {name:'test_2'}, ]; let str = 'test'; let n = 1; if (array.filter((item) => item.name === str).length) { str += "_" + n; } console.log(str); // test_1 - not uniqe, it already exist in array, it must be test_3 – risky Mar 18 '22 at 21:50
  • @risky I do not understand the convention you expect for the unique values, so I might be wrong, but I have edited my answer to ensure that the value is unique. If this is still not meeting your needs, then you will need to clarify the needs further. – Lajos Arpad Mar 19 '22 at 14:29
-2
function placeHolder(arg) {
    for (let i = 0; i < arg.length; i++) {
        if(parseInt(arg[i].name.slice(-1))) {
            continue;
        } else {
            arg[i].name = arg[i].name + (i + 1) 
        }
    }

    return arg;
}
Kerod
  • 1
  • 2